[Link to this article]
Http://www.cnblogs.com/hellogiser/p/reorder-list.html
[Question]
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 {, 7}, reorder it to {, 4 }.
[Analysis]
The question is more straightforward:
(1) locate the intermediate node of the linked list and divide the Linked List into two sublinked lists. If the length of the original linked list is an odd number, the length of the first sublinked list is 1 more;
(2) flip the second sublinked list;
(3) merge two sublinked lists.
Example: {1, 2, 3, 4, 5, 6, 7}
(1) Find the intermediate node of the linked list as 4, and divide the Linked List into two sub-linked lists: {1, 2, 4} and {5, 6, 7 };
(2) flip the second sub-linked list to get {7, 6, 5}
(3) Merge {,} and {, 5} to obtain {, 4}
[Code]
C ++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
// 62_ReorderList.cpp: Defines the entry point for the console application. // /* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: */ # Include "stdafx. h"
Struct ListNode { Int value; ListNode * next; };
// Find middle node of list ListNode * FindMiddleNode (ListNode * head) { If (NULL = head) Return NULL; ListNode * fast = head, * slow = head; While (fast! = NULL & fast-> next! = NULL) { // Move fast 2 steps Fast = fast-> next; If (fast = NULL) Break; // Move slow 1 step Slow = slow-> next; } Return slow; }
// Reverse list ListNode * ReverseList (ListNode * head) { If (NULL = head | NULL = head-> next) Return head; ListNode * prev = NULL, * cur = head, * next = NULL; While (cur! = NULL) { // Save next Next = cur-> next; // Reverse Cur-> next = prev; // Update prev and cur Prev = cur; Cur = next; } Return prev; }
// Cross merge list ListNode * CrossMergeList (ListNode * head1, ListNode * head2) { If (NULL = head1) Return head2; Else if (NULL = head2) Return head1;
ListNode * node1 = head1, * node2 = head2; While (node2! = NULL) { ListNode * temp1 = node1-> next; ListNode * temp2 = node2-> next; Node1-> next = node2; Node2-> next = temp1; // Update node1 node2 Node1 = temp1; Node2 = temp2; } Return head1; }
// Reorder list ListNode * ReOrderList (ListNode * head) { If (NULL = head | NULL = head-> next) Return head;
// Find middle node of list ListNode * middle = FindMiddleNode (head ); // Split into 2 lists ListNode * head1 = head; ListNode * head2 = middle-> next; // Detach the 2 lists Middle-> next = NULL; // Reverse list2 Head2 = ReverseList (head2 ); // Cross merge 2 lists Return CrossMergeList (head1, head2 ); } |
[Reference]
Http://blog.csdn.net/whuwangyi/article/details/14146461
[Link to this article]
Http://www.cnblogs.com/hellogiser/p/reorder-list.html