143. Reorder List
来源:互联网 发布:冰箱离墙距离 知乎 编辑:程序博客网 时间:2024/05/29 01:55
/*Given a singly linked list L: L0?L1?…?Ln-1?Ln,reorder it to: L0?Ln?L1?Ln-1?L2?Ln-2?…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {1,4,2,3}思路: 把链表从中间分成两个链表,后半部分链表反序,然后重组链表*//** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */void reorderList(struct ListNode* head){ if(!head || !head->next) return ;struct ListNode *firstPart=head;struct ListNode *secondPart=head;struct ListNode *fast=head,*mid=head;while(fast->next && fast->next->next)//把链表分成两部分fast=fast->next->next,mid=mid->next;secondPart=mid->next;mid->next=NULL;reverseList(&secondPart);//把第二部分链表反序fast=head,mid=secondPart;struct ListNode *tmpf,*tmpm;while(mid) //重新连接成一个链表{tmpf=fast->next;fast->next=mid;fast=tmpf;tmpm=mid->next;mid->next=tmpf;mid=tmpm;}}void reverseList(struct ListNode** head){if(!(*head) || !(*head)->next)return ;struct ListNode **h=head;struct ListNode *cur=*h,*pre;pre=cur->next;while(pre){cur->next=pre->next;pre->next=*h;*h=pre;pre=cur->next;}}
阅读全文
0 0
- [LeetCode]143.Reorder List
- LeetCode 143. Reorder List
- 143. Reorder List
- [leetcode] 143.Reorder List
- 143. Reorder List
- 143. Reorder List
- leetcode 143. Reorder List
- 143. Reorder List LeetCode
- 143. Reorder List
- LeetCode 143. Reorder List
- 143. Reorder List
- 143. Reorder List
- LeetCode 143. Reorder List
- LeetCode 143. Reorder List
- 143. Reorder List
- 143. Reorder List
- LeetCode *** 143. Reorder List
- 143. Reorder List
- lintcode:二叉树的前序遍历
- Java中的工厂模式
- shell学习(三)-crontab命令
- 设计模式(2) 代理模式 静态代理
- 利用json方式完成工具提示功能
- 143. Reorder List
- 完全背包问题
- 插入排序-直接插入排序
- mysql分库分表
- 【Linux】Linux中借助lockf机制,保证只有一个任务进程在执行
- POJ1952 BUY LOW, BUY LOWER(DP,最长下降子序列不重复计数)
- css鼠标点击的五种状态
- ReactiveSwift源码解析(十一) Atomic的代码实现以及其中的Defer延迟、Posix互斥锁、递归锁
- 版本控制系统GIT和SVN的区别