Reverse Linked List
To solve this coding challenge, you will need to reverse a singly linked list either iteratively or recursively and then return the reversed list. Here's how you can approach this problem:
pointer needs to point to the previous node instead of the next node, effectively reversing the direction of the list. You can achieve this by following the steps below for an iterative and a recursive solution.
pointer:
Explanation
In order to reverse a linked list, each nodeβsnext
Iterative Method
The iterative method involves traversing the list once and changing the direction of each node'snext
-
Initialize three pointers:
prev
None
curr
head
next_temp
- Iterate through the list. For each node:
-
Store the
next
-
Reverse the
next
prev
-
Move the
prev
-
Move the
curr
next_temp
-
After the loop,
prev
-
Return
prev
-
Base case: If the list is empty (
head
None
head.next
None
head
- Recursion: For the rest of the list, make a recursive call to reverse the sublist starting from the second node.
-
After the recursive call, reverse the
next
-
Set the
next
None
- Return the new head which is the result of the recursive call.
Recursive Method
The recursive method leverages the call stack to reverse the list. Here's a step-by-step explanation:Detailed Steps in Pseudocode
Iterative Approach
# Definition for singly-linked list node
class ListNode:
# Node constructor initializing value and next pointer
function init(val, next=None):
this.val = val
this.next = next
# Function to reverse the singly linked list iteratively
function reverseList(head):
# Initialize previous node to None
prev = None
# Initialize current node to head of the list
curr = head
# Loop until current node is None (end of the list)
while curr is not None:
# Store next node temporarily
next_temp = curr.next
# Reverse the current node's pointer to previous node
curr.next = prev
# Move previous node one step forward (to current node)
prev = curr
# Move current node one step forward (to next_temp)
curr = next_temp
# At the end, prev will be the new head of the reversed list
return prev
Recursive Approach
# Definition for singly-linked list node
class ListNode:
# Node constructor initializing value and next pointer
function init(val, next=None):
this.val = val
this.next = next
# Function to reverse the singly linked list recursively
function reverseListRecursive(head):
# Base case: if the list is empty or has only one node, return head
if head is None or head.next is None:
return head
# Recursive call to reverse the sublist starting from second node
new_head = reverseListRecursive(head.next)
# Reverse the next pointer of the second node to point to first node
head.next.next = head
# Set the next pointer of the first node to None (end of list)
head.next = None
# Return the new head of the reversed list
return new_head
These pseudocode explanations provide a clear step-by-step guide to reversing a singly linked list using both iterative and recursive methods. The comments within the pseudocode make it easy to follow the logic for reversing the links within the list nodes.