-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPartition.cpp
66 lines (65 loc) · 2.01 KB
/
Partition.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
/*
Write code to partition a linked list around a value X, such that all nodes less than X come
before all nodes greater than X. If X is contained within the list, then those nodes need
to be after the elements less than X and before the elements greater than X,
i.e. they should appear between the left and right partitions.
*/
class Solution
{
ListNode *partition(ListNode *head, int x)
{
ListNode *temp = head;
ListNode *left_head = NULL;
ListNode *left_tail = NULL;
ListNode *right_head = NULL;
ListNode *right_tail = NULL;
while (temp != NULL)
{
if (temp->val < x)
{
ListNode *newNode = new ListNode(temp->val);
if (left_head == NULL)
{
left_head = newNode;
left_tail = newNode;
}
else
{
newNode->next = left_head;
left_head = newNode;
}
}
else if (temp->val == x)
{
ListNode *newNode = new ListNode(temp->val);
if (left_head == NULL)
{
left_head = newNode;
left_tail = newNode;
}
else
{
left_tail->next = newNode;
left_tail = newNode;
}
}
else
{
ListNode *newNode = new ListNode(temp->val);
if (right_head == NULL)
{
right_head = newNode;
right_tail = newNode;
}
else
{
right_tail->next = newNode;
right_tail = newNode;
}
}
temp = temp->next;
}
left_tail->next = right_head;
return left_head;
}
}