Skip to content

Commit c66f44b

Browse files
authoredOct 24, 2020
Merge pull request #1308 from sameersrivastava13/dev
Solved #163 added : partition list leet code
2 parents 1fff8fe + 6948053 commit c66f44b

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
9+
10+
struct ListNode* partition(struct ListNode* head, int x){
11+
12+
struct ListNode *small, *large;
13+
struct ListNode *p = head;
14+
struct ListNode *s,*l;
15+
small = large = NULL;
16+
17+
if(p==NULL)
18+
{
19+
return p;
20+
}
21+
else{
22+
while(p!=NULL)
23+
{
24+
if(p->val<x)
25+
{
26+
if(small != NULL)
27+
{
28+
small->next = p;
29+
p = p->next;
30+
small = small->next;
31+
small->next = NULL;
32+
}
33+
else{
34+
small = p;
35+
p = p->next;
36+
small->next = NULL;
37+
s = small;
38+
}
39+
}
40+
else{
41+
if(large != NULL)
42+
{
43+
large->next = p;
44+
p = p->next;
45+
large = large->next;
46+
large->next = NULL;
47+
}
48+
else{
49+
large = p;
50+
p = p->next;
51+
large->next = NULL;
52+
l = large;
53+
}
54+
}
55+
}
56+
}
57+
if(large==NULL)
58+
{
59+
return head;
60+
}
61+
else if(small==NULL){
62+
return head;
63+
}
64+
else{
65+
small->next = l;
66+
head = s;
67+
return head;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Partition List
2+
3+
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
4+
5+
You should preserve the original relative order of the nodes in each of the two partitions.
6+
7+
## Example
8+
9+
`Input: head = 1->4->3->2->5->2, x = 3
10+
Output: 1->2->2->4->3->5`

0 commit comments

Comments
 (0)
Please sign in to comment.