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

[LeetCode 024] Swap Nodes in Pairs

2016-02-18 04:08 585 查看

Swap Nodes in Pairs



Implementation

/**
* 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) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode current = dummy;
while (current.next != null && current.next.next != null) {
ListNode p1 = current.next;
ListNode p2 = current.next.next;
p1.next = p2.next;
p2.next = p1;
current.next = p2;
current = p1;
}
return dummy.next;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: