0ms Java submision


  • 0
    J
    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode node1 = head;
            ListNode node2;
            ListNode temp;
            ListNode pre = null;
            while(node1 != null){
                node2 = node1.next;
                if(node2 == null) return head;
                temp = node2.next;
                node2.next = node1;
                node1.next = temp;
                if(pre != null){
                    pre.next = node2;
                }else{
                    head = node2;
                }
                pre = node1;
                node1 = pre.next;
            }
            return head;
        }
    }

Log in to reply
 

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