My AC java solution


  • 1
    Z
    /**
     * 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 p = new ListNode(0);
            p.next=head;
            ListNode curr = p;
            ListNode tmp = p;
            while(curr.next!=null && curr.next.next!=null){
                tmp=curr.next;
                curr.next=curr.next.next;
                tmp.next=curr.next.next;
                curr.next.next=tmp;
                curr=curr.next.next;
            }
            return p.next;
            
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.