Partition List

Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]
Input: head = [2,1], x = 2
Output: [1,2]
  • The number of nodes in the list is in the range [0, 200].
  • -100 <= Node.val <= 100
  • -200 <= x <= 200
def partition(self, head, x):
small,large=[],[]
temp=head
while(temp!=None):
if(temp.val<x):
small.append(temp.val)
else:
large.append(temp.val)
temp=temp.next
small=small+large
temp=head
index=0
while(temp!=None):
temp.val=small[index]
temp=temp.next
index+=1
return head
def partition(self, head, x):
small=ListNode(0)
large=ListNode(0)
small_pointer=small
large_pointer=large
while(head!=None):
if(head.val<x):
small.next=head
small=head
else:
large.next=head
large=head
head=head.next
large.next=None
small.next=large_pointer.next
return small_pointer.next

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

12 Followers

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.