Posts Partition List
Post
Cancel

Partition List

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.

leetcode

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * 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 new linked lists to hold nodes less than x and nodes greater than or equal to x.
        
        ListNode lessHead = new ListNode(101); // Placeholder value, will be ignored.
        ListNode lessTail = lessHead; // Pointer to the tail of the "less than x" list.

        ListNode moreHead = new ListNode(101); // Placeholder value, will be ignored.
        ListNode moreTail = moreHead; // Pointer to the tail of the "greater than or equal to x" list.

        ListNode t = head; // Temporary pointer to traverse the original linked list.

        while(t != null){

            ListNode next = t.next; // Save the next node before modifying current node's next pointer.

            // If the current node's value is less than x, append it to the "less than x" list.
            if(t.val < x){
                lessTail.next = t; // Add current node to the end of the "less than x" list.
                t.next = null; // Break the next pointer of the current node.
                lessTail = t; // Move the tail pointer of the "less than x" list to the current node.
            // If the current node's value is greater than or equal to x, append it to the "greater than or equal to x" list.
            } else {
                moreTail.next = t; // Add current node to the end of the "greater than or equal to x" list.
                t.next = null; // Break the next pointer of the current node.
                moreTail = t; // Move the tail pointer of the "greater than or equal to x" list to the current node.
            }

            t = next; // Move to the next node in the original list.
        }

        // Connect the tail of the "less than x" list to the head of the "greater than or equal to x" list. (skipping the dummy note)
        lessTail.next = moreHead.next;
        
        // The "less than x" list now contains all nodes less than x in their original order,
        // followed by all nodes greater than or equal to x in their original order.
        return lessHead.next; // Return the head of the merged list.
    }
}
This post is licensed under CC BY 4.0 by the author.