Location>code7788 >text

Daily Algorithmic Musings: Inverted Chained Tables

Popularity:781 ℃/2024-09-10 15:19:41

Problem solving:invert the linked list

This question asks us to invert a single linked table and return the reversed linked table. The reversal of a linked table can be done with theiteration (math.) cap (a poem)recursive (calculation) Two methods are used to realize it. Below we will explain these two methods in detail and demonstrate each step of the change process with examples.

Method 1: Iterative method

reasoning

  • We use three pointers to accomplish the reversal of the chain table:prev denotes the previous node.curr denotes the current node.next Indicates the next node.
  • By continually adding the current node'snext pointer on a gaugeprev, realizing the gradual inversion of a chained table.

Steps of Iteration

  1. initializationprev = null respond in singingcurr = head, and then start traversing the chain table.
  2. In each iteration, start withnext save (a file etc) (computing), avoiding chain table breaks.
  3. commander-in-chief (military) directionalprev, reversing the pointing of the current node.
  4. commander-in-chief (military)prev move tocurrand then thecurr move tonext, continue to the next iteration.
  5. (coll.) fail (a student)curr because ofnull When the chain table reversal is complete, theprev is the new head node.

code implementation

class Solution {
    public ListNode reverseList(ListNode head) {
        
        ListNode curr = head; while (curr !
        while (curr ! curr = head; while (curr ! = null) {
            ListNode next = ; // Save the next node.
             = prev; // reverse the current node's pointing direction
            prev = curr; // move prev forward
            curr = next; // move curr forward
        curr = next; // move curr forward }
        return prev; // prev becomes the head node of the new list.
    }
}

Example Demo

importationhead = [1,2,3,4,5]

  1. Initial state:

    prev = null
    curr = 1 -> 2 -> 3 -> 4 -> 5
    
  2. Step one:

    next = 2 -> 3 -> 4 -> 5
     = prev  // 1 -> null
    prev = 1 -> null
    curr = 2 -> 3 -> 4 -> 5
    
  3. Step two:

    next = 3 -> 4 -> 5
     = prev  // 2 -> 1 -> null
    prev = 2 -> 1 -> null
    curr = 3 -> 4 -> 5
    
  4. Step Three:

    next = 4 -> 5
     = prev  // 3 -> 2 -> 1 -> null
    prev = 3 -> 2 -> 1 -> null
    curr = 4 -> 5
    
  5. Step Four:

    next = 5
     = prev  // 4 -> 3 -> 2 -> 1 -> null
    prev = 4 -> 3 -> 2 -> 1 -> null
    curr = 5
    
  6. Step Five:

    next = null
     = prev  // 5 -> 4 -> 3 -> 2 -> 1 -> null
    prev = 5 -> 4 -> 3 -> 2 -> 1 -> null
    curr = null
    

exports[5, 4, 3, 2, 1]

complexity analysis

  • time complexity: O(n), wheren is the number of nodes in the chain table. We traversed the chain table only once.
  • (math.) space complexity: O(1), using only the constant level of extra space.

Method 2: Recursive method

reasoning

  • We recursively reverse subsequent parts of the chain table until the last node becomes the new head node.
  • On each recursive return, the next node in the current node'snext Pointing to himself, he simultaneously turned hisnext Set to null to complete the inversion.

recursive step

  1. in the event thathead because ofnull or because ofnullThe direct return of thehead as the new head node (i.e., the termination condition of the recursion).
  2. Recursively reverse the remaining chain list.
  3. Set the current node's next node'snext Pointing to himself, he simultaneously turned hisnext Set to empty.
  4. Returns the new header node.

code implementation

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || == null) return head; // recursive termination condition
        ListNode newHead = reverseList(); // Reverse the subsequent chain list
         = head; // point the following node to itself
         = null; // disconnect the current node from its successor
        return newHead; // return new head node
    }
}

Example Demo

importationhead = [1,2,3,4,5]

  1. Initial recursion:

    • reverseList(1) recursive callreverseList(2)
  2. recursively to the last node:

    • reverseList(5) come (or go) back5 as the new head node.
  3. Step-by-step reversal of a chained table:

    • reverseList(4)

      head = 4 -> 5
      After reversal 5 -> 4
       = 4
       = null // 4 -> null
      Returns 5 -> 4 -> null
      
    • reverseList(3)

      head = 3 -> 4 -> null
      After reversal 5 -> 4 -> 3
       = 3
       = null
      Returns 5 -> 4 -> 3 -> null
      
    • reverseList(2)

      head = 2 -> 3 -> null
      After reversal 5 -> 4 -> 3 -> 2
       = 2
       2 = 2 -> 2 -> 2 -> null
      Returns 5 -> 4 -> 3 -> 2 -> null
      
    • reverseList(1)

      head = 1 -> 2 -> null
      After reversal 5 -> 4 -> 3 -> 2 -> 1
       = 1
       1 = 1
      Returns 5 -> 4 -> 3 -> 2 -> 1 -> null
      

exports[5, 4, 3, 2, 1]

complexity analysis

  • time complexity: O(n), wheren is the number of nodes in the chain table. Each node is processed only once during the recursion.
  • (math.) space complexity: O(n), the stack depth of the recursive call isn

Summary:

  • iterative method: Stepwise reversal of a chained table by three pointers, with both O(n) and O(1) time and space complexity, suitable for use in space-critical scenarios.
  • recursive method: Recursion using the function call stack is clean and intuitive, but requires O(n) extra space.