Junedayday Blog

六月天天的个人博客

0%

Go算法实战 - 11.【反转链表LeetCode-206】

Go-Leetcode

Leetcode-206 反转链表

原题链接 https://leetcode-cn.com/problems/reverse-linked-list/

1
2
3
4
5
6
7
8
type ListNode struct {
Val int
Next *ListNode
}

func reverseList(head *ListNode) *ListNode {

}

题解

递归

1
2
3
4
5
6
7
8
9
10
func reverseList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
var p = reverseList(head.Next)
// 重点:调整两个前置指针
head.Next.Next = head
head.Next = nil
return p
}

非递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func reverseList(head *ListNode) *ListNode {
// 这里初始为nil,解决了第一个head指向为nil
var pre *ListNode
for head != nil {
// 存一下next指针,防止丢失
next := head.Next
// head指向前驱节点
head.Next = pre
// 两个指针往后挪,注意先后顺序
pre = head
head = next
}
return pre
}

Github: https://github.com/Junedayday/code_reading

Blog: http://junes.tech/

Bilibili: https://space.bilibili.com/293775192

公众号: golangcoding

二维码