forked from anshuman8800/Interivew-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartitionList.cpp
More file actions
29 lines (25 loc) · 758 Bytes
/
partitionList.cpp
File metadata and controls
29 lines (25 loc) · 758 Bytes
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
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* small = new ListNode(-1);
ListNode* large = new ListNode(-1);
ListNode* small_head = small;
ListNode* large_head = large;
while (head){
if (head->val < x){
small->next = head;
small = small -> next;
head = head -> next;
small->next = NULL;
}
else{
large->next = head;
large = large -> next;
head = head -> next;
large -> next = NULL;
}
}
small -> next = large_head -> next;
return small_head -> next;
}
};