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

Swap Nodes in Pairs

2016-06-07 07:54 387 查看
又是基本功,链表的基本操作。

注意犯错的地方

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