Easy to understand with Java solution


  • 0
    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode p = head.next, pre = head;
            while(pre != null && p != null){
                int tmp = p.val;
                p.val = pre.val;
                pre.val = tmp;
                if(p.next == null){
                    pre = null;
                    p = null;
                }else{
                    pre = p.next;
                    p = p.next.next;
                }
                
            }
            
            return head;
        }
    }

  • 0
    T

    Submission Result: Wrong Answer


Log in to reply
 

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