单链表翻转的几种写法
来源:互联网 发布:交换机端口电压 编辑:程序博客网 时间:2024/06/14 07:41
/* * 带头节点 */ListNode * reverse(ListNode *head) {if (head == NULL || head->next == NULL)return head;ListNode nhead(-1);//头节点nhead.next = head;ListNode *prev = head;ListNode *next = head->next;while (next != NULL) {prev->next = next->next;next->next = nhead.next;nhead.next = next;next = prev->next;}return nhead.next;}//不带头结点ListNode* reverse2(ListNode *head){if(head == NULL || head->next == NULL)return head;ListNode *prev = head;ListNode *cur = prev->next;ListNode *next = cur->next;while(cur != NULL){cur->next = prev;prev = cur;cur = next;next = next ? next->next : NULL;}head->next = NULL;return prev;}
ListNode *reverseList(ListNode *head) {ListNode *pre = NULL, *next = NULL;while (head) {next = head->next;head->next = pre;pre = head;head = next;}return pre;}
0 0
- 单链表翻转的几种写法
- 爱的几种写法
- 几种分页的写法
- 几种分页的写法
- singleton 的几种写法
- delegate的几种写法
- 登录的几种写法
- javascript 的几种写法
- Activity的几种写法
- 最大公约数的几种写法
- javascript的几种写法
- hql的几种写法
- hql的几种写法
- 倒计时的几种写法
- JS的几种写法
- 对字符串翻转的几种方法
- 关于字符串翻转的几种方法
- 单例模式的几种写法(包含双检锁写法)
- HDU 1556 Color the ball(线段树)
- PASSION之CSS常用操作
- 0-C相关01:NSlog函数介绍。
- MPICH2在windows7下的并行计算与拓展项目
- I/O流实现图片压缩
- 单链表翻转的几种写法
- 移植u-boot-2015.07-rc3之修改代码支持串口(三)
- jquery下的ajax
- 二叉树的基本使用
- SSM的maven项目
- window上安装 MongoDB PHP扩展
- Altium Designer静态标注的bug
- IO流
- 中文GCC