Leetcode学习笔记:#24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list’s nodes, only nodes itself may be changed.
实现:
public ListNode swapPairs(ListNode head){
if((head == null) || (head.next == null))
return head;
ListNode n = head.next;
head.next = swapPairs(head.next.next);
n.next = head;
return n;
}
思路:
递归。
该博客是Leetcode学习笔记,针对#24. Swap Nodes in Pairs题目,要求给定链表交换每两个相邻节点并返回头节点,且不能修改节点值,仅能改变节点本身,实现思路采用递归。

2567

被折叠的 条评论
为什么被折叠?



