LeetCode 143. Reorder List(重组链表)
2016-05-26 02:08
375 查看
原题网址:https://leetcode.com/problems/reorder-list/
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
方法一:使用栈来反转。
方法二:分三步,先找中间点,再反转后半部分,然后合并。
另一种实现:
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. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public void reorderList(ListNode head) { if (head == null) return ; ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } Stack<ListNode> stack = new Stack<>(); ListNode half = slow.next; while (half != null) { stack.push(half); half = half.next; } slow.next = null; ListNode current = head; while (current != null && !stack.isEmpty()) { ListNode next = current.next; current.next = stack.pop(); current.next.next = next; current = next; } } }
方法二:分三步,先找中间点,再反转后半部分,然后合并。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { private ListNode reverse(ListNode head) { if (head == null || head.next == null) return head; ListNode previous = head; ListNode current = head.next; head.next = null; ListNode reversed = null; do { reversed = current; ListNode next = current.next; current.next = previous; previous = current; current = next; } while (current != null); return reversed; } public void reorderList(ListNode head) { if (head == null) return ; ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } ListNode half = reverse(slow.next); slow.next = null; ListNode current = head; while (current != null && half != null) { ListNode cnext = current.next; ListNode hnext = half.next; current.next = half; half.next = cnext; current = cnext; half = hnext; } } }
另一种实现:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public void reorderList(ListNode head) { if (head == null || head.next == null || head.next.next == null) return; ListNode former = head; ListNode formerCurrent = former; ListNode slow = head; ListNode fast = head.next; while (fast != null && fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } ListNode latter = slow.next; slow.next = null; ListNode latterCurrent = latter.next; latter.next = null; while (latterCurrent != null) { ListNode next = latterCurrent.next; latterCurrent.next = latter; latter = latterCurrent; latterCurrent = next; } latterCurrent = latter; while (formerCurrent != null && latterCurrent != null) { ListNode formerNext = formerCurrent.next; ListNode latterNext = latterCurrent.next; formerCurrent.next = latterCurrent; if (formerNext != null) latterCurrent.next = formerNext; formerCurrent = formerNext; latterCurrent = latterNext; } } }
相关文章推荐
- swift版QQ音乐播放器(二)
- javascript学习之js使用小技巧
- codeforces #300 D. Weird Chess
- 半路出家的仓库管理员如何成为一个好的前端工程师(一)
- LeetCode 333. Largest BST Subtree
- 深入浅出 - Android系统移植与平台开发(十三) - Sensor HAL框架分析之三
- 深入浅出 - Android系统移植与平台开发(十四) - Sensor HAL框架分析之四
- 深入浅出 - Android系统移植与平台开发(十二) - Sensor HAL框架分析之二
- LeetCode 144. Binary Tree Preorder Traversal(二叉树前缀遍历)
- 深入浅出 - Android系统移植与平台开发(十一) - Sensor HAL框架分析之一
- SQL注入式攻击
- HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)
- HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)
- Eclipse工具安装OpenExplorer插件--快速打开文档目录
- iOS开发之点触textView编辑区关闭键盘
- HashMap深度解析(二)
- 写了一个基于React+Redux的仿Github进度条
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
- Activity启动模式