LeetCode全集请参考:LeetCode Github 大全
题目
24.两两交换链表中的节点
https://leetcode-cn.com/problems/swap-nodes-in-pairs/
/*
* @lc app=leetcode.cn id=24 lang=java
*
* [24] 两两交换链表中的节点
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
// check edge
if (head == null || head.next == null) {
return head;
}
ListNode n = head.next;
head.next = swapPairs(head.next.next);
n.next = head;
return n;
}
}
// @lc code=end