NOTE

Reverse Linked List

LeetCode notes on Reverse Linked List.

Data Structures & AlgorithmsCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Problem Description

Reverse a singly linked list.

2. Approach

  1. Approach 1
    • Three pointers
    • prev、current、next
    • As long as current is not nil, keep moving forward and reverse one link at each step

3. Implementation

3.1. Three Pointers

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reverseList(head *ListNode) *ListNode {
    var pre *ListNode
    current := head
    var next *ListNode
    for current != nil {
        next = current.Next
        current.Next = pre
        pre = current
        current = next
    }

    return pre
}

4. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub