Leetcode-Reorder List
2014-04-18 15:08
357 查看
作者:disappearedgod
文章出处:/article/3730084.html
时间:2014-8-20
Total Accepted: 9237 Total
Submissions: 47615My Submissions
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
调试文件
Solution(持续更新,java>c++)
文章出处:/article/3730084.html
时间:2014-8-20
题目
Reorder List
Total Accepted: 9237 TotalSubmissions: 47615My Submissions
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; 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); } }
返回
LeetCodeSolution(持续更新,java>c++)
相关文章推荐
- 【Leetcode】Reorder List
- Leetcode: Reorder List
- LeetCode Reorder List
- LeetCode之“链表”:Reorder List
- Reorder List --LeetCode
- (Leetcode 143 )Reorder List
- leetcode之Reorder List
- [leetcode][list] Reorder List
- LeetCode之 Reorder List解决思路
- LeetCode :: Reorder List
- Java for LeetCode 143 Reorder List
- 【leetcode】Reorder List
- LeetCode:Reorder List C++与Java实现
- [LeetCode] Reorder List 链表重排序
- [LeetCode 第9题] -- Reorder List
- Leetcode Reorder List
- Leetcode: Reorder List
- LeetCode:Reorder List
- 【leetcode每日一题】Reorder List
- [leetcode] Reorder List