-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1308 from sameersrivastava13/dev
Solved #163 added : partition list leet code
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
LinkedList/LinkedListPartition/python/partition-leet-code.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* struct ListNode *next; | ||
* }; | ||
*/ | ||
|
||
|
||
struct ListNode* partition(struct ListNode* head, int x){ | ||
|
||
struct ListNode *small, *large; | ||
struct ListNode *p = head; | ||
struct ListNode *s,*l; | ||
small = large = NULL; | ||
|
||
if(p==NULL) | ||
{ | ||
return p; | ||
} | ||
else{ | ||
while(p!=NULL) | ||
{ | ||
if(p->val<x) | ||
{ | ||
if(small != NULL) | ||
{ | ||
small->next = p; | ||
p = p->next; | ||
small = small->next; | ||
small->next = NULL; | ||
} | ||
else{ | ||
small = p; | ||
p = p->next; | ||
small->next = NULL; | ||
s = small; | ||
} | ||
} | ||
else{ | ||
if(large != NULL) | ||
{ | ||
large->next = p; | ||
p = p->next; | ||
large = large->next; | ||
large->next = NULL; | ||
} | ||
else{ | ||
large = p; | ||
p = p->next; | ||
large->next = NULL; | ||
l = large; | ||
} | ||
} | ||
} | ||
} | ||
if(large==NULL) | ||
{ | ||
return head; | ||
} | ||
else if(small==NULL){ | ||
return head; | ||
} | ||
else{ | ||
small->next = l; | ||
head = s; | ||
return head; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Partition List | ||
|
||
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. | ||
|
||
You should preserve the original relative order of the nodes in each of the two partitions. | ||
|
||
## Example | ||
|
||
`Input: head = 1->4->3->2->5->2, x = 3 | ||
Output: 1->2->2->4->3->5` |