1. Find mid point and break it into two lists

2. reverse the second list (mid to end)

3. reinsert into first one.

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     void reorderList(ListNode *head) {
12         if (!head || !head->next) return;
13         ListNode *slow = head, *fast = head;
14         while (fast->next && fast->next->next) {
15             slow = slow->next;
16             fast = fast->next->next;
17         }
18         fast = slow->next;
19         slow->next = NULL;
20         slow = NULL;
21         while (fast) {
22             ListNode *tmp = fast->next;
23             fast->next = slow;
24             slow = fast;
25             fast = tmp;
26         }
27         fast = head;
28         while (slow) {
29             ListNode *tmp = slow;
30             slow = slow->next;
31             tmp->next = fast->next;
32             fast->next = tmp;
33             fast = fast->next->next;
34         }
35     }
36 };

 

posted on 2015-03-23 05:31  keepshuatishuati  阅读(137)  评论(0编辑  收藏  举报