Title:
Given a singly linked listL:L0→L1→... →L N-1→LN,
Reorder it to:L0→LN→L1→L N-1→L2→L N-2→...
You must does this in-place without altering the nodes ' values.
For example,
Given {1,2,3,4} , reorder it to {1,4,2,3} .
Ideas:
We can first find the midpoint of the list (using the fast and slow pointer), then flip the second half of the list, and link to the first half.
Example: such as 1-2-3-4-5-null, we flip the 4-5-null, the first half of the linked list 3-null, and then link 1-2-3-null, 5-4-null, in turn, the latter part of the flip list is inserted, get 1-5-2-4-3-null.
Attention:
1. Note that when the linked list is divided into two parts, the tail node of the first part is set to NULL
(2) Link after half of the position of the node as the chain tail slow->next = NULL;
2. When flipping a linked list, we should construct a virtual head node, because it is flipped from start to finish, so each time it is inserted into the virtual head node, the original head is continuously moved back. Of course, we have to first determine whether the second half of the list conforms to the rollover condition, or if there is a node.
if (head = = NULL | | head->next = = NULL) return;
3. Note that when we get the middle node through a fast and slow pointer, the head node of the second part of the list should be slow->next.
listnode* Reversehead = slow->next;
4. Note that we only insert the nodes in the rollover list into the first half of the linked list when the node is flipped.
while (reversehead! = NULL)
5. Note the details of the insertion, we are the next node that retains the second half of the linked list header nodes first. Re-link.
Flip the list if there is a node on the Continue link while (reversehead! = NULL) { //save after the half-linked list after a node listnode* tmp = reversehead->next; Connecting two linked lists Reversehead->next = cur->next; Cur->next = Reversehead; Determine the new starting point for links to two linked lists reversehead = tmp; cur = cur->next->next; }
Complexity: O (N)
AC Code:
/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *}; */class Solution {public:void reorderlist (ListNode *head) {if (head = = NULL | | head->next = NULL) return; (1) Find half of the list after a node listnode* slow = head; listnode* fast = head; while (fast! = NULL && Fast->next! = null) {slow = slow->next; Fast = fast->next->next; } listnode* Reversehead = slow->next; (2) Link after half of the position of the node as the chain tail slow->next = NULL; (3) Flip linked list reverse_list (reversehead); (4) Link two linked list listnode* cur = head; Flip the list if there is a node on the Continue link while (reversehead! = NULL) {//Save after the half-linked list after a node listnode* tmp = Reverseh ead->next; Connecting two linked lists Reversehead->next = cur->next; Cur->next = Reversehead; IndeedA new starting point for the link of the two linked list reversehead = tmp; Cur = cur->next->next; } return; }private://Flip list void Reverse_list (listnode* &head) {if (head = = NULL | | head->next = NULL) ret Urn ListNode dummyhead (0); Dummyhead.next = head; listnode* p = head; while (p->next! = NULL) {listnode* tmp = p->next; P->next = tmp->next; Tmp->next = Dummyhead.next; Dummyhead.next = tmp; } head = Dummyhead.next; Return }};
[C + +] leetcode:75 Reorder List