输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
代码实现
// ListNode Definition for singly-linked list.
type ListNode struct {
Val int
Next *ListNode
}
func reverseList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
var prev *ListNode
cur := head
for cur != nil {
cur.Next, prev, cur = prev, cur, cur.Next
}
return prev
}