-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0086. Partition List.cpp
More file actions
50 lines (43 loc) · 1.71 KB
/
0086. Partition List.cpp
File metadata and controls
50 lines (43 loc) · 1.71 KB
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
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
// create a new node at the start to decrease the complexity of the rest of the code.
// It will be deleted at the end.
head = new ListNode(-1000, head);
// we first need to find the first node >= x.
ListNode* nextNodeIsTarget = head;
while (nextNodeIsTarget->next && nextNodeIsTarget->next->val < x) nextNodeIsTarget = nextNodeIsTarget->next;
// return early if no moves have to happen
if (!nextNodeIsTarget->next) {
ListNode* temp = head->next;
delete head;
return temp;
}
// now we go through the list after the node, and move all nodes less than it to before nextNodeIsTarget->next
ListNode* traverser = nextNodeIsTarget->next;
while (traverser->next != nullptr) {
if (traverser->next->val < x) {
// remove the small node past the target node
ListNode* temp = traverser->next;
traverser->next = temp->next;
// put the small node in the right location
temp->next = nextNodeIsTarget->next;
nextNodeIsTarget->next = temp;
nextNodeIsTarget = nextNodeIsTarget->next;
} else {
traverser = traverser->next;
}
}
ListNode* temp = head->next;
delete head;
return temp;
}
};