Leetcode-Reorder List
来源:互联网 发布:数据分析的网站有哪些 编辑:程序博客网 时间:2024/06/05 14:57
作者:disappearedgod
文章出处:http://blog.csdn.net/disappearedgod/article/details/24019729
时间:2014-8-20
题目
Reorder List
Total Accepted: 9237 Total Submissions: 47615My SubmissionsGiven 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; next = null; } }
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public void reorderList(ListNode head) { if(head == null || head.next == null) return; ListNode p = head; ListNode reversal = new ListNode(head.val); ListNode rp = new ListNode(head.val);//这里为了形成一个环 所以先new一个点,而不是null,在for里面形成这个点 ListNode tmp = null; int length = 0; for(p = head.next; p !=null; p = p.next){ tmp = new ListNode(p.val); tmp.next = rp; rp = tmp;//这两句形成环 如果p = head 的情况下 length++; } reversal = rp; //printList(reversal); p = head; rp = reversal; head = rp; ListNode t_pN = null; ListNode t_rpN = null; for(int i = 0; i < length/2; i++){ t_pN = p.next; t_rpN = rp.next; p.next = rp; rp.next = t_pN; p = t_pN; rp = t_rpN; } if(p != null) if(length%2!=0 && p.next !=null) p.next.next = null; else p.next = null; }}
调试文件
package List;/** * Created by yuyan on 14-8-20. */public class ReorderList extends ListTest { @Override protected void work(ListNode head) { super.work(head); if(head == null || head.next == null) return; ListNode p = head; ListNode reversal = new ListNode(head.val); ListNode rp = new ListNode(head.val); ListNode tmp = null; int length = 0; for(p = head.next; p !=null; p = p.next){ tmp = new ListNode(p.val); tmp.next = rp; rp = tmp; length++; } reversal = rp; p = head; rp = reversal; ListNode t_pN = null; ListNode t_rpN = null; for(int i = 0; i < length/2; i++){ t_pN = p.next; t_rpN = rp.next; p.next = rp; rp.next = t_pN; p = t_pN; rp = t_rpN; } if(p != null) if(length%2!=0 && p.next !=null) p.next.next = null; else p.next = null; } public static void main(String[] args){ ReorderList test = new ReorderList(); int[] a = {1,2,3}; ListNode head = test.buildTestListFromArray(a); //test.printList(head); test.work(head); test.printList(head); }}
package List;/** * Created by yuyan on 14-8-20. */public class ListTest { protected void work(ListNode head){ } protected void printList(ListNode head){ if(head == null){ System.out.print("[ ]"); return; } for(; head.next !=null; head = head.next){ System.out.print("["+ head.val+"]->"); } System.out.print("["+ head.val+"]"); } protected ListNode buildTestListFromArray(int[] a){ ListNode testList = new ListNode(a[0]); ListNode p = testList; for(int i = 1 ; i < a.length; i++){ ListNode tmp = new ListNode(a[i]); p.next = tmp; p = p.next; } return testList; } protected ListNode buildDescendTestList(int N){ ListNode testList = null; for(int i = 0 ; i < N; i++){ ListNode tmp = new ListNode(i+1); tmp.next = testList; testList = tmp; } return testList; } protected ListNode buildAscendTestList(int N){ ListNode testList = new ListNode(1); ListNode p = testList; for(int i = 1 ; i < N; i++){ ListNode tmp = new ListNode(i+1); p.next = tmp; p = p.next; } return testList; } public static void main(String[] args){ ListTest t = new ListTest(); ListNode testList = t.buildAscendTestList(10); t.printList(testList); }}
返回
LeetCode Solution(持续更新,java>c++)
0 0
- [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)
- spring3 + Quartz1.8.6 Schuduler Example
- 读jQuery Blog之New Demo Page
- .NET Framework 的 Quirk Version
- B. Garland
- 数学之路(3)-SVR-支持向量回归
- Leetcode-Reorder List
- UIKit 06、使用UITabBarController 和 自定义标签栏控件 创建多视图的iOS应用程序
- Sony 面经
- 【分享】国家自然科技资源平台实验材料数据库
- leetcode之时间复杂度为O(nlogn)的链表排序
- 如何重用其他项目中的源代码
- Android之Alarm
- errno perrno与strerrno
- 键盘聚焦