当前位置: 首页 > news >正文

免费手工活外发加工网站中国建设招标网网站首页

免费手工活外发加工网站,中国建设招标网网站首页,php网络公司网站源码,网站建设好了怎么在百度可以搜到反转单链表 题目描述 题目分析 先来说迭代的思想#xff1a; 上面next cur-next应该放在cur-next pre前面执行#xff0c;这里笔误 再来说递归的思想#xff1a; 题目代码 这个代码里面我加了我自己写的测试数据#xff0c;自己可以去找对应的部分#xff0c…反转单链表 题目描述 题目分析  先来说迭代的思想 上面next cur-next应该放在cur-next pre前面执行这里笔误  再来说递归的思想 题目代码 这个代码里面我加了我自己写的测试数据自己可以去找对应的部分直接粘贴赋值就可以在leetcode提交成功我们就没有把迭代与递归单独分开了里面还有大量的注释说明请结合题目分析仔细观看 java版本 package com.pxx.test;import sun.awt.image.ImageWatched;class LinkList {private ListNode head;private int length;//长度//链表结点的一个定义static class ListNode {int value;ListNode next;//初始化一个结点public ListNode(int value) {this.value value;this.next null;}}public ListNode getHead() {return this.head;}//实现数据的尾插public void insertEnd(int value) {ListNode newNode new ListNode(value);if (newNode ! null) {if (head null) {head newNode;//指向第一个结点} else {ListNode cur head;//移动到最后一个结点的位置while (cur.next ! null) {cur cur.next;}//最后一定是在cur加入结点cur.next newNode;}length;}}//打印public void printList(ListNode head) {ListNode cur head;while (cur ! null) {System.out.print(cur.value );cur cur.next;}System.out.println();}//利用递归反转链表//返回反转之后的链表头public ListNode reverse(ListNode head) {//安全检查都必须先进行操作if (head null || head.next null) {return head;} else {ListNode pre head , cur head.next;//接收最后一次的返回就是最后一个节点中间是没有结点要返回的//这里为什么传入pre.next而不是cur.next//目的其实就是为了每一个值都能被链上为什么那么说呢//1(pre) 2(cur) 3 4 5//如果按照cur.next来进行移动//1 - 2这一部分递归了之后就会进入 3(pre(cur会直接到3变成pre)) 4内部的cur//那么我请问2 3中间这条线被狗吃了吗所以以pre.next往下递归//上面pre循环到5号结点也就是最后一组递归就要结束因此有head.next null就结束递归ListNode res reverse(pre.next);cur.next pre;//目的把第一个结点的next指向null//这里千万不能吧pre.next 与 cur.next进行比较//否则会造成最开头的两个数据一直轮替//这样来说 1 2 3 4(pre) 5cur假设当前指针是这样来指的//4 - 5 此时4也还是4-5的进入pre.next把指向变为了null注意这是递归所以从后往前分析//不太懂的请结合我的递归分析图来看//null - 4 - 5//那么就行往前递归3(pre) 4(cur) 5,也就是null - 3 - 4 - 5//我们注意到一个问题就是4的pre已经在中间的时候被改向了第一个结点//那么对于1(pre) - 2(cur)来说,1的.next又等于cur,所以1.pre null//null - 1 - 2......这个时候程序已经全部执行完if (pre.next cur) {pre.next null;}return res;}}//利用迭代思想实现public ListNode reverse1(ListNode head) {//空结点直接返回一个nullif (head null) {return null;}//定义三个指针进行迭代ListNode pre head, cur head.next, next;//next用于在中间轮替指针可以先不用初值//没循环之前先把第一个结点next指向nullhead.next null;while (cur ! null) {//这里迭代就是// null - 1(pre) 2(cur) 3(next) 4 5// null - 1 - 2(pre) 3(cur) 4(next) 5// null - 1 - 2 - 3(pre) 4(cur) 5(next)// null - 1 - 2 - 3 - 4(pre) 5(cur) next(这个时候还会进入循环里面更改cur.next指针)//这个时候cur null,结束,pre指向最后一个结点返回//保留next指向next cur.next;//这一步必须放在cur.next前面不然cur.next就被先改变了,next的位置就不对了cur.next pre;//改变pre与curpre cur;//这一步放在curnext,这里就是先赋值pre,在改变curcur next;}//最后一定是pre指向了最后一个结点return pre;}}public class Test4 {public static void main(String[] args) {LinkList linkList new LinkList();//开始插入数据linkList.insertEnd(1);linkList.insertEnd(2);linkList.insertEnd(3); // linkList.insertEnd(4); // linkList.insertEnd(5);linkList.printList(linkList.getHead());//反转链表LinkList.ListNode head linkList.reverse(linkList.getHead());linkList.printList(head);} }c语言版本测试代码仅做参考 #include stdio.h #include stdlib.h// 链表结点的定义 struct ListNode {int value;struct ListNode* next; };// 链表的定义 struct LinkedList {struct ListNode* head;int length; };// 初始化链表 void initLinkedList(struct LinkedList* list) {list-head NULL;list-length 0; }// 在链表末尾插入一个新节点 void insertEnd(struct LinkedList* list, int value) {struct ListNode* newNode (struct ListNode*)malloc(sizeof(struct ListNode));if (newNode ! NULL) {newNode-value value;newNode-next NULL;if (list-head NULL) {list-head newNode;} else {struct ListNode* cur list-head;while (cur-next ! NULL) {cur cur-next;}cur-next newNode;}list-length;} }// 打印链表的元素 void printList(struct ListNode* head) {struct ListNode* cur head;while (cur ! NULL) {printf(%d , cur-value);cur cur-next;}printf(\n); }// 递归反转链表 struct ListNode* reverse(struct ListNode* head) {if (head NULL || head-next NULL) {return head;} else {struct ListNode* pre head;struct ListNode* cur head-next;struct ListNode* res reverse(pre-next);cur-next pre;if (pre-next cur) {pre-next NULL;}return res;} }int main() {struct LinkedList linkList;initLinkedList(linkList);// 开始插入数据insertEnd(linkList, 1);insertEnd(linkList, 2);insertEnd(linkList, 3);printList(linkList.head);// 反转链表linkList.head reverse(linkList.head);printList(linkList.head);return 0; }好了祝早安午安晚安
http://www.pierceye.com/news/84657/

相关文章:

  • 正宗营销型网站建设mui做的h5网站案例
  • 网站开发现在主要用什么语言wordpress权限配置文件
  • ftp 修改网站环境设计网站推荐
  • 政务网站建设方案太原网络推广公司
  • php 用什么做网站服务器网站程序系统
  • 下载正品官方网站做音箱木工网站
  • 网站模板下载 免费什么是企业营销型网站
  • 网站优化知识wordpress ping百度
  • 免费申请一个网站竞价可以做两个网站吗
  • 公众号版网站建设设计师培训哪家好
  • 郑州网站开发的公司电话培训类网站开发
  • 怎样做公司网站营销活动策划方案模板
  • 网站不兼容怎么办微分销商城
  • 桥梁建设 网站客户管理系统服务
  • html商务网站模板宠物网站建设内容
  • 浙江有限公司网站小公司网站如何做
  • 网站制作哪个软件做货代用的网站
  • 网站客户端怎么做的wordpress注册目录
  • 大连建设主管部门官方网站wordpress主题制作详解
  • 官方微网站吗中原彼得堡航空学院网站的建设
  • 怎么做网站站长网页美工设计实训
  • 建设网站需求上海手机网站建设公司
  • 有哪些做网站的天元建设集团有限公司汇票信誉
  • 网站白名单 是什么wordpress全站静态页面
  • php高级网站开发淮安建设工程协会网站查询
  • 做网站域名备案需要多久赣州哪里可以做网站
  • 网站代码优化怎么做网站建设意见征求汇报
  • 北京市的重点门户网站有哪些广州网站建设哪家比较好
  • 军队 网站备案网络运营托管公司
  • 自己做微博的网站怎么做神马搜索排名seo