bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 86. Partition List

86. Partition List

Leetcode Solutions

Two Pointer Approach

  1. Initialize two dummy nodes before_head and after_head to serve as the heads of the two new linked lists.
  2. Use two pointers before and after to point to the current last node of the before and after lists, respectively.
  3. Iterate through the original linked list.
  4. For each node, if its value is less than x, append it to the before list.
  5. If the node's value is greater than or equal to x, append it to the after list.
  6. After the iteration, connect the before list to the after list by setting before.next to after_head.next.
  7. Return before_head.next as the new head of the partitioned linked list.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...