Swap Nodes in Pairs

来源:互联网 发布:windows 10 redstone 编辑:程序博客网 时间:2024/06/01 23:11

又是基本功,链表的基本操作。

注意犯错的地方

/** * 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;    }}


0 0