-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartition List.cpp
39 lines (38 loc) · 1.07 KB
/
Partition List.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
//Partition List
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/*
consider that find the nodes that are less than x -- first linked list
all the elements are greater and equal to x -- second linked list
point the 1st ->next to 2nd list
return the 1st list
*/
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(head == NULL) return head;
ListNode *dummy1 = new ListNode(-1);
ListNode *dummy2 = new ListNode(-1);
ListNode *node1 = dummy1;
ListNode *node2 = dummy2;
while(head){
if(head -> val < x){
node1 -> next = head;
node1 = node1 -> next;
}else{
node2 -> next = head;
node2 = node2 -> next;
}
head = head -> next;
}
node2 -> next = NULL; // end flag
node1 -> next = dummy2 -> next; // point dummy2 to the end of node1 list
return dummy1 -> next;
}
};