Advertisement

Partition List - LeetCode 86 Solution

Partition List - Complete Solution Guide

Partition List is LeetCode problem 86, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. 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] Constraints: The number of nodes in the list is in the range [0, 200] . -100 <= Node.val <= 100 -200 <= x <= 200

Detailed Explanation

The problem asks us to rearrange a given linked list such that all nodes with values less than a given value 'x' appear before nodes with values greater than or equal to 'x'. The original relative order of nodes within each partition (less than x and greater than or equal to x) must be preserved. The input is the head of the linked list and the value 'x'. The output is the head of the modified linked list.

Solution Approach

The solution utilizes two separate linked lists to store nodes less than 'x' and nodes greater than or equal to 'x', respectively. We iterate through the original linked list and append each node to the appropriate list. Dummy head nodes are used to simplify the process of creating the two new lists. After the partitioning is complete, the 'greater than or equal to' list is terminated by setting its tail's next pointer to NULL. Finally, the 'less than' list is connected to the 'greater than or equal to' list, forming the partitioned linked list. The dummy head nodes are then discarded by returning the next node of the less_head.

Step-by-Step Algorithm

  1. Step 1: Initialize two dummy nodes, `less_head` and `greater_head`, and corresponding tail pointers, `less_tail` and `greater_tail`, pointing to these dummy nodes. These will serve as the heads and tails of the 'less than x' and 'greater than or equal to x' partitions.
  2. Step 2: Iterate through the original linked list using a `current` pointer.
  3. Step 3: For each node `current`, check if its value is less than `x`. If it is, append it to the 'less than x' list by setting `less_tail.next = current` and moving `less_tail` to `current`.
  4. Step 4: If the node `current`'s value is greater than or equal to `x`, append it to the 'greater than or equal to x' list by setting `greater_tail.next = current` and moving `greater_tail` to `current`.
  5. Step 5: After processing the entire original list, terminate the 'greater than or equal to x' list by setting `greater_tail.next = None`. This is crucial to prevent a cycle or incorrect linkage.
  6. Step 6: Connect the 'less than x' list to the 'greater than or equal to x' list by setting `less_tail.next = greater_head.next`. This joins the two partitions together.
  7. Step 7: Return the head of the partitioned list, which is `less_head.next`. This skips the dummy `less_head` node.

Key Insights

  • Insight 1: Using two separate linked lists (or pointers acting as heads/tails) allows efficient partitioning without needing to swap elements in place, which would be cumbersome for linked lists.
  • Insight 2: Creating dummy nodes for the 'less than' and 'greater than or equal to' lists simplifies the logic, especially when handling the initial empty state of these lists.
  • Insight 3: Crucially, after partitioning, the 'next' pointer of the last node in the 'greater than or equal to' list must be set to NULL to avoid forming a cycle or including nodes from the original list that shouldn't be there.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Linked List, Two Pointers.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Microsoft, tcs.