Given a reference head of type ListNode that is the head of a singly linked list, reorder the list in-place such that the nodes are reordered to form the following pattern:
1st node -> last node -> 2nd node -> 2nd to last node -> 3rd node ...
Example 1:input:
output:
51423head
Example 2:input:
012head
output:
021head
Solution
The first observation is that we can create our final list by merging two smaller lists: the first half of the original list and the reversed second half of the original list.
For example, if our original list is 5 -> 4 -> 3 -> -> 2 -> 1, then the first half is 5 -> 4 -> 3 and the reversed second half is 1 -> 2. We can merge these two lists to get the final list 5 -> 1 -> 4 -> 2 -> 3.
54312Above: The first half of the original listBelow: The reversed second half of the original list
With that established, we can focus on getting the reverse of the 2nd half of the list. This breaks down to first finding the middle node of the list, and then reversing the direction of the nodes starting from the middle node to the end of the list.
Put together, the solution involves three steps, each of which involve 3 fairly common linked list operations:
Finding the middle of the linked list (using fast and slow pointers).
Reversing the nodes between the middle and the end of the linked list.
Merging the first half of the linked list with the reversed second half.
Step 1: Find the Middle of the Linked List
This step can be done using fast and slow pointers, which involves initializing two pointers, fast and slow, at the head of the linked list, and then iterating until fast reaches the end of the list. At each iteration, slow moves one node forward and fast moves two nodes forward. When fast reaches the tail node of the list, slow will point to the middle node in the list.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
54321slowfast
initialize pointers
0 / 2
Python
When there are an even number of nodes, fast will equal to None after moving past the tail node, and slow will be the first node of the second half of the list. In the case below, there are 4 nodes, so slow will point to the 3rd node.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
5443slowfast
initialize pointers
0 / 2
Python
Step 2: Reverse second half of list
At this point, slow points to the middle node in the list. Next, we want to reverse the direction of the pointers of each node starting from slow to the tail of the list.
Reversing the nodes in a linked list is a common problem that can be solved by iterating over each node that needs to be reversed. The key idea is to maintain three pointers, prev, curr, and next_, where prev points to the previous node, curr points to the node with the pointer we want to reverse, and next_ points to the next node in the iteration.
At each iteration, we:
save the next node in the iteration by setting next_ = curr.next
reverse the pointer by setting curr.next = prev
move pointers for the next iteration by set curr = next_ and prev = curr
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
54321slowfast
fast = fast.next.next, slow = slow.next
0 / 10
Python
You need the next_pointer to store the next node in the iteration before overwriting curr.next = prev. If you don't store the next node in the iteration, you will lose the reference to the rest of the linked list.
Step 3: Merge first half with reversed second half
At this point, when curr is None, then prev will be the head of the reversed second half of the list. We can then merge the first half of the list with the reversed second half by iterating over the two halves and updating the pointers of the nodes.
We can do so by initializing two pointers: first, which points to the head of the first half of the list, and second, which points to the head of the reversed second half of the list, which is initially prev.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
54321prevcurrnext_
curr = next_, prev = curr
0 / 1
Python
From there, we want to merge the nodes at first and second together, with first coming before second. To do so, we first set first.next = second. And since we are over-writing first.next, we need to simultaneously advance first = first.next so that we have access to the next node in the first half of the list.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
54321firstsecond
first = head, second = prev
0 / 1
Python
Now the first node in the original list and the first node in the reversed second half are connected, so we need to connect the 2nd node in the original list with the first node in the reversed second half. We can do so by setting second.next = first and making sure we simultaneously advance second = second.next so that we have access to the next node in the reversed second half.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
54321firstsecond
first.next = 1, first = 4
0 / 1
Python
At this point, the first three nodes in our original linked list are correctly ordered, and first and second are pointing to the next nodes in each half that we need to merge together. So we can continue the merge until second has reached the end of the reversed second half of the list, at which point we can return None.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
54321firstsecond
second.next = 4, second = 2
0 / 3
Python
Implementation
Here's the complete 3-step solution that reorders the list in-place:
defreorderList(head):
ifnot head ornot head.next:
return
# Step 1: Find the middle of the list using slow/fast pointers
slow = head
fast = head
while fast.nextand fast.next.next:
slow = slow.next
fast = fast.next.next
# Step 2: Reverse the second half starting from slow.next
second_half = reverse_list(slow.next)
slow.next=None# Cut the list into two halves
# Step 3: Merge two halves alternately
first_half = head
while second_half:
first_next = first_half.next# Store next nodes
second_next = second_half.next
first_half.next= second_half # Link first to second
second_half.next= first_next # Link second to first's next
first_half = first_next # Move to next nodes
second_half = second_next
defreverse_list(head):
prev =None
current = head
while current:
next_temp = current.next
current.next= prev
prev = current
current = next_temp
return prev
Python
Code
To construct the linked list that is used in the animation below, provide a list of integers nodes. Each integer in nodes is used as the value of a node in the linked list, and the order of the integers in the list will be the order of the nodes in the linked list.
For example, if nodes = [1, 2, 3], the linked list will be 1 -> 2 -> 3.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
54321
reorder list
0 / 19
Python
Edge Cases
Empty List
When the linked list is empty, the initial check for head being None will return None immediately.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
reorder list
0 / 1
Python
head = []
Single Node
When the linked list has one node, the initial check for head.next being None will return head immediately.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
1
reorder list
0 / 1
Python
head = [1]
Two Nodes
When the linked list has two nodes, the linked list is already in the correct order.
The while loop to find the middle node iterates once, with slow pointing to the 2nd node.
The while loop to reversing the second half runs once, but since there is only one node in the 2nd half of the list, no pointers are updated, and prev points to the 2nd node.
The while loop to merge the two halves doesn't run, as second.next is None.
defreorderList(head):
ifnot head ornot head.next:
return head
# find middle node
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse second half of list
prev, curr =None, slow
while curr:
next_ = curr.next
curr.next= prev
prev, curr = curr, next_
# merge first and reversed second half of list
first, second = head, prev
while second.next:
first.next, first = second, first.next
second.next, second = first, second.next
return head
12
reorder list
0 / 8
Python
head = [1, 2]
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n) where n is the number of nodes in the linked list. We iterate over the linked list three times: once to find the middle node, once to reverse the second half of the list, and once to merge the two halves together. Each iteration takes O(n) time.
Space Complexity: O(1) We only use a constant amount of extra space to store pointers and temporary variables. Regardless if the linked list has 10 nodes or 10,000 nodes, we will still use two pointers to find the middle node, three pointers to reverse the second half of the list, and two pointers to merge the two halves together.
Your account is free and you can post anonymously if you choose.