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's
next
pointer on a gaugeprev
, realizing the gradual inversion of a chained table.
Steps of Iteration:
- initialization
prev = null
respond in singingcurr = head
, and then start traversing the chain table. - In each iteration, start with
next
save (a file etc) (computing), avoiding chain table breaks.
- commander-in-chief (military)
directional
prev
, reversing the pointing of the current node. - commander-in-chief (military)
prev
move tocurr
and then thecurr
move tonext
, continue to the next iteration. - (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:
importation:head = [1,2,3,4,5]
-
Initial state:
prev = null curr = 1 -> 2 -> 3 -> 4 -> 5
-
Step one:
next = 2 -> 3 -> 4 -> 5 = prev // 1 -> null prev = 1 -> null curr = 2 -> 3 -> 4 -> 5
-
Step two:
next = 3 -> 4 -> 5 = prev // 2 -> 1 -> null prev = 2 -> 1 -> null curr = 3 -> 4 -> 5
-
Step Three:
next = 4 -> 5 = prev // 3 -> 2 -> 1 -> null prev = 3 -> 2 -> 1 -> null curr = 4 -> 5
-
Step Four:
next = 5 = prev // 4 -> 3 -> 2 -> 1 -> null prev = 4 -> 3 -> 2 -> 1 -> null curr = 5
-
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), where
n
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's
next
Pointing to himself, he simultaneously turned hisnext
Set to null to complete the inversion.
recursive step:
- in the event that
head
because ofnull
orbecause of
null
The direct return of thehead
as the new head node (i.e., the termination condition of the recursion). - Recursively reverse the remaining chain list.
- Set the current node's next node's
next
Pointing to himself, he simultaneously turned hisnext
Set to empty. - 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:
importation:head = [1,2,3,4,5]
-
Initial recursion:
-
reverseList(1)
recursive callreverseList(2)
。
-
-
recursively to the last node:
-
reverseList(5)
come (or go) back5
as the new head node.
-
-
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), where
n
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 is
n
。
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.