https://leetcode-cn.com/problems/swap-nodes-in-pairs/
按照新链的从后向前赋值.
Golang
func swapPairs(head *ListNode) *ListNode {
resultHead := &ListNode{Val: -1, Next: head}
tail := resultHead
for tail != nil {
tail = helper(tail)
}
return resultHead.Next
}
// return next start node
func helper(head *ListNode) *ListNode {
if head == nil || head.Next == nil || head.Next.Next == nil {
return nil
}
tempFirst := head.Next
tempSecond := head.Next.Next
tempFirst.Next = tempSecond.Next
tempSecond.Next = tempFirst
head.Next = tempSecond
return head.Next.Next
}