86. Partition List

Medium

Problem Description

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.

Examples

Example 1:
Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]

Example 2:
Input: head = [2,1], x = 2
Output: [1,2]
Jump to Solution: Python Java C++ JavaScript C#

Python Solution


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
def partition(head: Optional[ListNode], x: int) -> Optional[ListNode]:
    # Create two dummy nodes for less and greater lists
    less_head = less = ListNode(0)
    greater_head = greater = ListNode(0)
    
    # Partition the list
    curr = head
    while curr:
        if curr.val < x:
            less.next = curr
            less = less.next
        else:
            greater.next = curr
            greater = greater.next
        curr = curr.next
    
    # Connect the two lists
    greater.next = None
    less.next = greater_head.next
    
    return less_head.next

Java Solution


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode partition(ListNode head, int x) {
        // Create two dummy nodes for less and greater lists
        ListNode lessHead = new ListNode(0);
        ListNode less = lessHead;
        ListNode greaterHead = new ListNode(0);
        ListNode greater = greaterHead;
        
        // Partition the list
        ListNode curr = head;
        while (curr != null) {
            if (curr.val < x) {
                less.next = curr;
                less = less.next;
            } else {
                greater.next = curr;
                greater = greater.next;
            }
            curr = curr.next;
        }
        
        // Connect the two lists
        greater.next = null;
        less.next = greaterHead.next;
        
        return lessHead.next;
    }
}

C++ Solution


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        // Create two dummy nodes for less and greater lists
        ListNode* lessHead = new ListNode(0);
        ListNode* less = lessHead;
        ListNode* greaterHead = new ListNode(0);
        ListNode* greater = greaterHead;
        
        // Partition the list
        ListNode* curr = head;
        while (curr) {
            if (curr->val < x) {
                less->next = curr;
                less = less->next;
            } else {
                greater->next = curr;
                greater = greater->next;
            }
            curr = curr->next;
        }
        
        // Connect the two lists
        greater->next = nullptr;
        less->next = greaterHead->next;
        
        ListNode* result = lessHead->next;
        delete lessHead;
        delete greaterHead;
        return result;
    }
};

JavaScript Solution


/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} x
 * @return {ListNode}
 */
var partition = function(head, x) {
    // Create two dummy nodes for less and greater lists
    const lessHead = new ListNode(0);
    let less = lessHead;
    const greaterHead = new ListNode(0);
    let greater = greaterHead;
    
    // Partition the list
    let curr = head;
    while (curr) {
        if (curr.val < x) {
            less.next = curr;
            less = less.next;
        } else {
            greater.next = curr;
            greater = greater.next;
        }
        curr = curr.next;
    }
    
    // Connect the two lists
    greater.next = null;
    less.next = greaterHead.next;
    
    return lessHead.next;
};

C# Solution


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode Partition(ListNode head, int x) {
        // Create two dummy nodes for less and greater lists
        ListNode lessHead = new ListNode(0);
        ListNode less = lessHead;
        ListNode greaterHead = new ListNode(0);
        ListNode greater = greaterHead;
        
        // Partition the list
        ListNode curr = head;
        while (curr != null) {
            if (curr.val < x) {
                less.next = curr;
                less = less.next;
            } else {
                greater.next = curr;
                greater = greater.next;
            }
            curr = curr.next;
        }
        
        // Connect the two lists
        greater.next = null;
        less.next = greaterHead.next;
        
        return lessHead.next;
    }
}

Complexity Analysis

Solution Explanation

This solution uses two pointers to create separate lists:

Key points: