Please can anyone provide any feedback on what is wrong with my logic?


  • 0
    F
    public class Solution {
        public ListNode swapPairs(ListNode head) {
              if(head == null){
    		   return null;
    	   }
    	   if(head.next == null){
    		   return head;
    	   }
    	   
    	   ListNode current = head;
    	   ListNode temp = head.next;
    	   head = head.next;
    	   
    	   while(temp != null){
    		   current.next = temp.next;
    		   temp.next = current;
    		   current = current.next;
    		   if(current == null)
    		   {
    			   return head;
    		   }
    		 temp = current.next;
    		   
    	   }
    	   return head;
            
        }
    }

Log in to reply
 

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