0

I was trying to swap the adjacent nodes of a linked list using queues. Below is the code:

class Solution {
    public ListNode swapPairs(ListNode head) {
        Queue<ListNode> q1 = new LinkedList<>();
        Queue<ListNode> q2 = new LinkedList<>();
        ListNode curr = head;
     
        while(curr != null && curr.next != null){
            q1.offer(curr);
            curr = curr.next.next;
        }
        curr = head.next;
        while(curr != null || !q1.isEmpty()){
            if(curr != null)
            q2.offer(curr);
            
            q2.offer(q1.poll());  //this line seems to be the problem
            
            if(curr.next != null)
            curr = curr.next.next;
            else
                curr = curr.next;
        }
        ListNode dummy = new ListNode(0);
        curr = dummy;
        while(!q2.isEmpty()){
            dummy.next = q2.poll();
            dummy = dummy.next;
        }
        return curr.next;
    }
}

I tried this but got an error that says: Found cycle in the ListNode. Please help. When I tried debugging, I found q2.offer(q1.poll()); seems to be causing the problem.

P.S. I know there is a simpler way to solve this question that is, a single iteration and using pointers. But I am a little new to programming . So I am trying out things but cannot figure out why the above code gives an error.

msbtech
  • 21
  • 3
  • What about this example https://www.geeksforgeeks.org/swap-nodes-in-a-linked-list-without-swapping-data/ ? Check the code after `// If x is not head of linked list ` – Traycho Ivanov Aug 01 '20 at 19:36
  • In a Java queue, offer and poll return values, and are alternatives to add and remove that throw Exceptions instead. You aren't checking the return values. – NomadMaker Aug 01 '20 at 23:21
  • Got it.. thanks Traycho Ivanov and NomadMaker!! – msbtech Aug 02 '20 at 20:40

1 Answers1

0

Made some changes to your code. Found the issue when you are constructing List back from the queue.

public ListNode swapPairs(ListNode head) {
            Queue<ListNode> q1 = new LinkedList<>();
            Queue<ListNode> q2 = new LinkedList<>();
            ListNode curr = head;
         
            while(curr != null && curr.next != null){
                q1.offer(curr);
                curr = curr.next.next;
            }
           
           // If there are odd numbers in the list
            if (curr!=null)
                q1.offer(curr);
            
            curr = head.next;
            while(curr != null || !q1.isEmpty()){
                if(curr != null) {
                    System.out.println(curr.val);
                    q2.offer(curr);
                }
                 
                q2.offer(q1.poll());  
                
                if (curr == null)
                    continue;
                
                if(curr.next != null)
                    curr = curr.next.next;
                else
                    curr = curr.next;
            }
            

            curr = q2.poll();
            head = curr;
            
            while(!q2.isEmpty()){

                // create a new node
                ListNode dummy = new ListNode(0);
                dummy.val = q2.poll().val;
                dummy.next = null;
                
                curr.next = dummy;
                curr = dummy;               
            }
            
            return head;
}

Input: 1 2 3 4 5 
Output: 2 1 4 3 5

Input: 1 2 3 4
Output: 2 1 4 3
abhijit gupta
  • 161
  • 1
  • 12