NOTE
Reverse Linked List
LeetCode notes on Reverse Linked List.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Problem Description
Reverse a singly linked list.
2. Approach
- Approach 1
- Three pointers
- prev、current、next
- As long as
currentis 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
}
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub