206. 反转链表 golang

206. 反转链表

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

Code

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reverseList(head *ListNode) *ListNode {
	x := head
	y := head
	var z *ListNode
	temp := head
	for  head != nil {
		x = temp
		y = temp.Next

		x.Next = z

		if y == nil {
			break
		}
		temp = y
		z = x
	}
	return x
}
发布了399 篇原创文章 · 获赞 266 · 访问量 41万+

猜你喜欢

转载自blog.csdn.net/csdn_kou/article/details/105206062