forked from TechyGuyAditya/Hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition-List.cpp
More file actions
43 lines (37 loc) · 931 Bytes
/
Partition-List.cpp
File metadata and controls
43 lines (37 loc) · 931 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Leetcode question : 86. Partition List
class Solution
{
public:
ListNode *partition(ListNode *head, int x)
{
if (head == NULL || head->next == NULL)
return head;
// Create 2 nodes for 2 new linkedlist :
// 1. for low elemnts
// 2. for higher or equal elemsts
ListNode *low, *l, *high, *h;
low = new ListNode;
high = new ListNode;
l = low; // initialize
h = high;
while (head)
{
if (head->val < x)
{
l->next = head;
l = l->next;
head = head->next;
l->next = NULL;
}
else
{
h->next = head;
h = h->next;
head = head->next;
h->next = NULL;
}
}
l->next = high->next;
return low->next;
}
};