您的位置:首页 > Web前端 > Node.js

Swap Nodes in Pairs(交换链表相邻的两个结点)

2016-05-26 13:56 645 查看
交换链表相邻的两个结点
/**
* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null) return head;
ListNode list = head.next;
ListNode list1 = list.next;
list.next = head;
head.next = swapPairs(list1);
return list;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: