题解 | #两两交换链表的节点#
两两交换链表的节点
https://www.nowcoder.com/practice/71f95c23810349f782a1aa6c9bd714b4
import java.util.*;
public class Solution {
public ListNode swapLinkedPair (ListNode head) {
// write code here
//通过递归来做与反转链表类似,先逆置前两个,
//子问题函数:将第三个后面的全部两两逆置,传入头节点
//截至函数:
if(head==null||head.next==null){
return head;
}
ListNode nNode = swapLinkedPair(head.next.next);
ListNode ret = head.next;
//改变指针指向
ret.next = head;
head.next = nNode;
return ret;
}
}
