/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/// 定义 swapPairs 函数返回将以 head 为头的链表两两交换后的新头节点
funcswapPairs(head*ListNode)*ListNode{ifhead==nil||head.Next==nil{returnhead}else{newHead:=head.Nexthead.Next=swapPairs(newHead.Next)newHead.Next=headreturnnewHead}}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/funcswapPairs(head*ListNode)*ListNode{dummy:=&ListNode{}prev,cur:=dummy,headforcur!=nil{ifcur.Next==nil{prev.Next=curbreak}a,b:=cur,cur.Nextcur=b.Nextb.Next=aa.Next=nilprev.Next=bprev=a}returndummy.Next}