Simple JAVA Solution


  • 0
    R

    I used the simple swapping technique. head contains the current node and temp contains the previous node.

    public class Solution {
        public ListNode swapPairs(ListNode head) {
            ListNode start = new ListNode(0);
            start.next = head;
            ListNode temp = start;
            while(head!=null && head.next!=null){
                temp.next = head.next;
                head.next = head.next.next;
                temp.next.next = head;
                temp = head;
                head = head.next;
            }
            return start.next;
        }
    }
    

Log in to reply
 

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