Skip to content

Commit

Permalink
Merge pull request #1308 from sameersrivastava13/dev
Browse files Browse the repository at this point in the history
Solved #163 added  : partition list leet code
  • Loading branch information
i-vishi authored Oct 24, 2020
2 parents 1fff8fe + 6948053 commit c66f44b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
69 changes: 69 additions & 0 deletions LinkedList/LinkedListPartition/python/partition-leet-code.c
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;
}
}
10 changes: 10 additions & 0 deletions LinkedList/LinkedListPartition/python/readme.md
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`

0 comments on commit c66f44b

Please sign in to comment.