题目
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例:
给定 1->2->3->4, 你应该返回 2->1->4->3.
方案一(递归)
/*** Definition for singly-linked list.* type ListNode struct {* Val int* Next *ListNode* }*/func swapPairs(head *ListNode) *ListNode {if head == nil || head.Next == nil {return head}// 调整指针next := head.Nextnext_next := head.Next.Nextnext.Next = headhead.Next = next_nexthead.Next = swapPairs(next_next)return next}
原文
https://leetcode-cn.com/explore/featured/card/recursion-i/256/principle-of-recursion/1201/
