LeetCode Reorder List

 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  

12     ListNode* reverse(ListNode* head) {

13         if (head == NULL) {

14             return NULL;

15         }

16         ListNode* pre = NULL;

17         ListNode* cur = head;

18         while (cur != NULL) {

19             ListNode* tmp = cur->next;

20             cur->next = pre;

21             pre = cur;

22             cur = tmp;

23         }

24         return pre;

25     }

26 

27 

28     void reorderList(ListNode *head) {

29         if (head == NULL || head->next == NULL) {

30             return;

31         }

32         int num = 0;

33         ListNode* cur = head;

34         while (cur != NULL) {

35             num++;

36             cur = cur->next;

37         }

38         // seperate list, find middle node as the head of the new list

39         int ridx = num / 2;

40         ListNode* rhead = NULL;

41         cur = head;

42         for (int i=0; true; i++) {

43             if (i == ridx - 1) {

44                 rhead = cur->next;

45                 cur->next = NULL;

46                 break;

47             }

48             cur = cur->next;

49         }

50  

51         rhead = reverse(rhead);

52         

53         cur = head;

54         head = head->next;

55     

56         cur->next = rhead;

57         cur = rhead;

58         rhead = rhead->next;

59             

60         while (head != NULL && rhead != NULL) {

61             cur->next = head;

62             cur = head;

63             head = head->next;

64                 

65             cur->next = rhead;

66             cur = rhead;

67             rhead = rhead->next;

68         }

69         cur->next = rhead; // there must be only one node left(odd case) or NULL(even case)

70     }

71 

72 };

这题跟Copy List with Random Pointer 那题类似在生成结果前都对原先的链表结构进行了调整,这里就是把链表的后半段给逆序了一下,这样我们就能轻松的取出节点对(0, n), (1, n-1)...了(序号是原先未修改时的,[0, n])

 

第二轮:

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→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}.

 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  // 10:26

10 class Solution {

11 public:

12     void reorderList(ListNode *head) {

13         if (head == NULL) {

14             return;

15         }

16         ListNode fakeHead(0);

17         fakeHead.next = head;

18         ListNode* slow = &fakeHead;

19         ListNode* fast = &fakeHead;

20         while (fast != NULL && fast->next != NULL) {

21             slow = slow->next;

22             fast = fast->next->next;

23         }

24         ListNode* second = slow->next;

25         slow->next = NULL;

26         

27         ListNode* pre = NULL;

28         ListNode* cur = second;

29         while (cur != NULL) {

30             ListNode* tmp = cur->next;

31             cur->next = pre;

32             pre = cur;

33             cur = tmp;

34         }

35         ListNode* ahead = fakeHead.next;

36         ListNode* bhead = pre;

37         

38         fakeHead.next = NULL;

39         ListNode* last = &fakeHead;

40         

41         while (ahead != NULL && bhead != NULL) {

42             last->next = ahead;

43             last = ahead;

44             ahead = ahead->next;

45             last->next = bhead;

46             last = bhead;

47             bhead = bhead->next;

48         }

49         last->next = ahead;

50     }

51 };

 取中间点的方法改进了一下

 

你可能感兴趣的:(LeetCode)