Knowee
Questions
Features
Study Tools

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.

Question

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.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step solution in Python:

  1. Initialize two new linked lists, before and after. These will hold the nodes that are less than x and greater than or equal to x respectively.
before = before_head = ListNode(0)
after = after_head = ListNode(0)
  1. Iterate through the original linked list. For each node, check if its value is less than x. If it is, append it to the before list. If it's not, append it to the after list.
while head:
    if head.val < x:
        before.next = head
        before = before.next
    else:
        after.next = head
        after = after.next
    head = head.next
  1. After the iteration, the after list might still have a reference to some of the nodes in the before list. To break this reference, set the next node of after to None.
after.next = None
  1. Then, connect the before list with the after list.
before.next = after_head.next
  1. The before_head now is the head of the partitioned linked list. Return before_head.next.
return before_head.next

Here is the complete code:

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def partition(head, x):
    before = before_head = ListNode(0)
    after = after_head = ListNode(0)

    while head:
        if head.val < x:
            before.next = head
            before = before.next
        else:
            after.next = head
            after = after.next
        head = head.next

    after.next = None
    before.next = after_head.next

    return before_head.next

This solution works in O(n) time complexity, where n is the number of nodes in the linked list, and O(1) space complexity, as we are not using any extra space that scales with the input size.

This problem has been solved

Similar Questions

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) Example 1:Input: head = [1,2,3,4]Output: [2,1,4,3]Example 2:Input: head = []Output: []Example 3:Input: head = [1]Output: [1] Constraints:The number of nodes in the list is in the range [0, 100].0 <= Node.val <= 100

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example 1:Input: head = [1,1,2]Output: [1,2]Example 2:Input: head = [1,1,2,3,3]Output: [1,2,3] Constraints:The number of nodes in the list is in the range [0, 300].-100 <= Node.val <= 100The list is guaranteed to be sorted in ascending order.

Given pointer to a node X in a singly linked list. Only one pointer is given, pointer to head node is not given, can we delete the node X from given linked list?Possible if X is not last node. Use following two steps (a) Copy the data of next of X to X. (b) Delete next of X.Possible if size of linked list is even.Possible if size of linked list is oddPossible if X is not first node. Use following two steps (a) Copy the data of next of X to X

A linked list whose last node points back to both the head node and the previous node instead of containing the null pointer ____________

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.