Golang Leetcode 206. Reverse Linked List.go

版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89012897

思路

反转链表是链表算法里面比较基础的,可以在纸上推导一下这个过程加深理解
重点是,反转的过程中不能丢失节点

code


type ListNode struct {
	Val  int
	Next *ListNode
}

func reverseList(head *ListNode) *ListNode {
	var newh ListNode
	for head != nil {
		newh.Next, head.Next, head = head, newh.Next, head.Next
	}
	return newh.Next

}

更多内容请移步我的repo: https://github.com/anakin/golang-leetcode

猜你喜欢

转载自blog.csdn.net/anakinsun/article/details/89012897