首页 > 其他 > 详细

LeetCode - Refresh - Reorder List

时间:2015-03-23 07:04:21      阅读:225      评论:0      收藏:0      [点我收藏+]

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 };

 

LeetCode - Refresh - Reorder List

原文:http://www.cnblogs.com/shuashuashua/p/4358817.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!