题目:定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
type Node struct {
val int
next *Node
}
func reverseList(head *Node) *Node {
if head == nil || head.next == nil {
return head
}
front, middle, tail := head.next.next, head.next, head
tail.next = nil
for front != nil {
middle.next = tail
tail = middle
middle = front
front = front.next
}
middle.next = tail
return middle
}