-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path143.重排链表.cpp
74 lines (71 loc) · 1.75 KB
/
143.重排链表.cpp
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
/*
* @lc app=leetcode.cn id=143 lang=cpp
*
* [143] 重排链表
*
* https://leetcode-cn.com/problems/reorder-list/description/
*
* algorithms
* Medium (52.51%)
* Likes: 120
* Dislikes: 0
* Total Accepted: 10.5K
* Total Submissions: 19.9K
* Testcase Example: '[1,2,3,4]'
*
* 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,
* 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…
*
* 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
*
* 示例 1:
*
* 给定链表 1->2->3->4, 重新排列为 1->4->2->3.
*
* 示例 2:
*
* 给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.
*
*/
// @lc code=start
/**
* 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){return ;}
ListNode* slow=head;
ListNode* fast=head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
}
ListNode* preNode = nullptr;
ListNode* p = slow->next;
slow->next = nullptr;
while(p){
ListNode* next = p->next;
p->next = preNode;
preNode = p;
p = next;
}
ListNode* res = new ListNode(-1);
while(preNode){
res->next = head;
head = head->next;
res = res->next;
res->next = preNode;
preNode = preNode->next;
res = res->next;
}
res->next = head;
head = res;
}
};
// @lc code=end