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:

Explanation

In order to reverse a linked list, each node’s
next
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.

Iterative Method

The iterative method involves traversing the list once and changing the direction of each node's
next
pointer:
  1. Initialize three pointers:
    prev
    as
    None
    ,
    curr
    as
    head
    (the starting node of the list), and
    next_temp
    to keep track of the next node in original list while reversing.
  2. Iterate through the list. For each node:
    • Store the
      next
      node.
    • Reverse the
      next
      pointer of the current node to point to the
      prev
      node.
    • Move the
      prev
      pointer one step forward to the current node.
    • Move the
      curr
      pointer to the
      next_temp
      node.
  3. After the loop,
    prev
    will be pointing to the new head of the list.
  4. Return
    prev
    .
  5. Recursive Method

    The recursive method leverages the call stack to reverse the list. Here's a step-by-step explanation:
  6. Base case: If the list is empty (
    head
    is
    None
    ) or has only one node (
    head.next
    is
    None
    ), simply return
    head
    because there's nothing to reverse.
  7. Recursion: For the rest of the list, make a recursive call to reverse the sublist starting from the second node.
  8. After the recursive call, reverse the
    next
    pointer of the second node to point back to the first node.
  9. Set the
    next
    pointer of the first node to
    None
    to mark the end of the reversed list.
  10. Return the new head which is the result of the recursive call.

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.