[LeetCode]Reorder List
来源:互联网 发布:照片绘画软件 编辑:程序博客网 时间:2024/06/15 10:22
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}
.
Analysis:
One straightforward middle step of such reordering is:
{1,2,3,4,5,6} --> {1,2,3,6,5,4} --> {1,6,2,5,3,4}
{1,2,3,4,5,6,7}---> {1,2,3,4,7,6,5} ---> {1,7,2,6,3,5,4}
By reversing the last half part of the linked list, we do not need to worried about the "parent" pointer anymore. The final step is just insert the each element in the last half part into the first part (every two element).
So the algorithm implemented below can be summarized as:
Step 1 Find the middle pointer of the linked list (you can use the slow/fast pointers)
Step 2 Reverse the second part of the linked list (from middle->next to the end)
Step 3 Do the reordering. (inset every element in the second part in between the elements in the first part)
java
public void reorderList(ListNode head) { if(head==null || head.next == null|| head.next.next == null) return; ListNode slow = head; ListNode fast = head; while(fast!=null&& fast.next!=null){ slow = slow.next; fast = fast.next.next; } //slow is in the middle, reverse slow.next to end fast = slow.next; slow.next = null; while(fast!=null){ ListNode temp = fast.next; fast.next = slow; slow = fast; fast = temp; }//merge two list ListNode first = head; ListNode second = slow; while(first!=null && first.next!=second && first!=second){ ListNode temp = second.next; second.next = first.next; first.next = second; first = second.next; second = temp; } }c++
void reorderList(ListNode *head) { if(head == NULL || head->next == NULL) return; //find half node ListNode *slow = head; ListNode *fast = head; while(true){ fast = fast->next; if(fast == NULL) break; fast = fast->next; if(fast == NULL) break; slow = slow->next; } //resever post half list ListNode *cur = slow; ListNode *pre = slow->next; cur->next = NULL; while(pre!=NULL){ ListNode *temp = pre->next; pre->next = cur; cur = pre; pre = temp; } // merge two list ListNode *first = head; ListNode *second = cur; while(first != second && first != NULL && second != NULL){ ListNode *temp = second->next; second->next = first->next; first->next = second; first = second->next; second = temp; } }
- [leetcode][list] Reorder List
- [leetcode][list] Reorder List
- Leetcode: Reorder List
- [LeetCode] Reorder List
- LeetCode题解:Reorder List
- Leetcode: Reorder List
- LeetCode:Reorder List
- leetcode之Reorder List
- Leetcode: Reorder List
- [LeetCode]Reorder List
- [LeetCode] Reorder List
- leetcode Reorder List
- LeetCode | Reorder List
- LeetCode Reorder List
- LeetCode 之 Reorder List
- Leetcode: Reorder List
- LeetCode OJ:Reorder List
- Reorder List (LeetCode)
- CentOS 6.5系统安装配置MySQL 5.6数据库
- ubuntu卸载redis
- NIO 入门
- TPLINK TL-WN725N 2.0在fedora 20下安装驱动
- sqlmap遇到星号怎么办?
- [LeetCode]Reorder List
- CentOS 6.4中vsftp安装与配置
- web开发中常用的js
- 百田笔试题
- Linux Shell 命令记录概略:二
- 蓝桥杯 约束倍数选卡片
- Java获取某年某月的第一天
- java使用安全的PreparedStatement操作mysql数据库(菜鸟入门级别的)
- EasyUI基础入门之Easyloader(加载器)