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.
Solution
Here is a step-by-step solution in Python:
- Initialize two new linked lists,
beforeandafter. These will hold the nodes that are less thanxand greater than or equal toxrespectively.
before = before_head = ListNode(0)
after = after_head = ListNode(0)
- Iterate through the original linked list. For each node, check if its value is less than
x. If it is, append it to thebeforelist. If it's not, append it to theafterlist.
while head:
if head.val < x:
before.next = head
before = before.next
else:
after.next = head
after = after.next
head = head.next
- After the iteration, the
afterlist might still have a reference to some of the nodes in thebeforelist. To break this reference, set the next node ofaftertoNone.
after.next = None
- Then, connect the
beforelist with theafterlist.
before.next = after_head.next
- The
before_headnow is the head of the partitioned linked list. Returnbefore_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.
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 ____________
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.