Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Partition List problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Partition List problem solution, we have given the head of 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.

Leetcode Partition List problem solution

Leetcode Partition List problem solution in Python.

class Solution:
    def partition(self, head: ListNode, x: int) -> ListNode:
        dummy1 = ListNode(0)
        head1 = dummy1
        dummy2 = ListNode(0)
        head2 = dummy2
        while head:
            if head.val < x:
                head1.next = head
                head1 = head1.next     
            else:
                head2.next = head
                head2 = head2.next
            head = head.next        
        head2.next = None
        head1.next = dummy2.next
        return dummy1.next

Partition List problem solution in Java.

public ListNode partition(ListNode head, int x) {
        if(head==null||head.next==null) return head;
        ListNode first=null;
        ListNode firstHead=null;
        ListNode secondHead=null;
        ListNode second=null;
        ListNode cur=head;
        while(cur!=null){
            if(cur.val<x){
                if(first==null) {first=cur; firstHead=first;}
                else {first.next=cur; first=first.next;}
            }
            else{
                if(second==null) {second=cur; secondHead=second;}
                else {second.next=cur; second=second.next;}
            }
            cur=cur.next;
        }
        
        if(first==null) {
            second.next=null;
            return secondHead;
        }
        else if(second==null){
            return firstHead;
           
            
        }else{
             second.next=null;
            first.next=secondHead;
        }
        return firstHead;
        
    }

Problem solution in C++.

ListNode* partition(ListNode* head, int target) {
	ListNode* fake_head = new ListNode(target - 1);
	fake_head->next = head;
	ListNode* previous = fake_head;
	ListNode* partition = fake_head;
	ListNode* current = head;
	while (current) {
		if (current->val < target) {
			if (previous->val < target) {
				previous = current;
			} else {
				previous->next = current->next;
				current->next = partition->next;
				partition->next = current;
			}
			partition = current;
		} else {
			previous = current;
		}
		current = previous->next;
	}
	head = fake_head->next;
	fake_head->next = NULL;
	delete fake_head;
	return head;
}

Problem solution in C.

struct ListNode* partition(struct ListNode* head, int x){
    struct ListNode *p, *q, node, node2;
    node.next = head;
    q = &node;
    p = &node2;
    while(q->next){
        if(q->next->val >= x){
            p->next = q->next;
            p = p->next;
            q->next = p->next;
        }
        else q = q->next;
    }
    p->next = NULL;
    q->next = node2.next;
    return node.next;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes