Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am solving leetcode problems and I can't figure out why my code is wrong. Here is the description of the problem:
Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.


And here is my code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
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;
        
    }
}


it works in all cases, but in the following:
C#
Input:  {1,2,3,4}
Output: {2,1,3}
Expected:   {2,1,4,3}


Can anyone help me please? I would really appreciate any type of feedback
Posted
Comments
Sergey Alexandrovich Kryukov 22-Oct-14 19:05pm    
One word: debugger...
One note: "it works on all cases but one" is an apparently false statement; do I even have to explain why?
—SA
Alexander24 22-Oct-14 19:08pm    
Probably you are right. But I have worked all the cases by hand and I was sure it works.
Sergey Alexandrovich Kryukov 22-Oct-14 20:02pm    
Debugger...
—SA
Alexander24 22-Oct-14 20:04pm    
I know that, I just did not want to write a whole program just to debug
Sergey Alexandrovich Kryukov 22-Oct-14 20:05pm    
This statement makes no sense at all. Maybe you don't even know the meaning of the word "debugger", or something like that. You don't have to write anything. You want do debug you non-working code, not I. But if you think that "writing a whole program" is something bad, you probably need to change your profession. Can we stop this useless discussion?
—SA

I have written two helpers to make testing easy.

C#
private ListNode GenerateList(int GenerateTo)
{
  int i = 1;
  ListNode myList = new ListNode(i);
  ListNode Tail = myList;
  while (i < GenerateTo)
  {
    i++;
    ListNode newTail = new ListNode(i);
    Tail.next = newTail;
    Tail = Tail.next;
  }
  return myList;
}


And I have added a method to print the list as an additional method to the ListNode class

C#
public class ListNode
 {
   int val;
   public ListNode next;
   public ListNode(int x)
   {
     val = x;
     next = null;
   }

   public string PrintList()
   {
     string result = string.Empty;
     if (next == null)
       result += string.Format("({0}) -> null", val);
     else
       result += string.Format("({0}) -> ", val) + next.PrintList();

     return result;
   }
 }


This made the tests very simple

C#
[TestMethod]public void TestMethod_Two()
    {
      ListNode testList = GenerateList(2);
      ListNode result = ClassUnderTest.swapPairs(testList);
      Assert.AreEqual("(2) -> (1) -> null", result.PrintList());
    }


    [TestMethod]
    public void TestMethod_One()
    {
      ListNode testList = GenerateList(1);
      ListNode result = ClassUnderTest.swapPairs(testList);
      Assert.AreEqual("(1) -> null", result.PrintList());
    }


My Suggestion that if you want this two work consider Regression as a solution.
 
Share this answer
 
Comments
PIEBALDconsult 22-Oct-14 23:39pm    
"consider Regression as a solution"

I hope you don't mean recursion.
Anyway had a chance to complete a solution that makes use of regression. To keep the logic clear I have probably created a few extra variables, but it wrote it to be clear in what it did. The advantage with this method that it only parses the list once so even with the extra variables it is vary efficient.

C#
public static ListNode swapPairsReg(ListNode List)
{
  if (List == null)
    return null;
  ListNode One = List;
  ListNode Two = One.next;

  if (Two == null)
    return One;
  ListNode tail = Two.next;

  ListNode NewList = Two;
  NewList.next = One;
  NewList.next.next = swapPairsReg(tail);
  return NewList;
}
 
Share this answer
 
Comments
Alexander24 23-Oct-14 12:37pm    
Your answer works perfectly, thank you Sir. However, Can you help me find out what is wrong with my logic???
Malcolm Chambers 23-Oct-14 14:40pm    
I am glad that you solved your method but do you agree that using recursion results in a solution that is much clearer.
Alexander24 23-Oct-14 16:35pm    
Yes, I totally agree with you. However, recursion is a topic that I'm not very comfortable with yet.
I finally fixed the problem. Here is my solution:
public class Solution {
    public ListNode swapPairs(ListNode head) {
      
      if(head == null){
		   return null;
	   }
	   if(head.next == null){
		   return head;
	   }
	   
	   ListNode temp = head.next;
	   ListNode current = head;
	   head = head.next;
	   ListNode prev = new ListNode(0);
	   
	  
	   while(temp != null){
		   current.next = temp.next;
		   temp.next = current;
		   prev.next = temp;
		   current = current.next;
		   if(current == null)
		   {
			   return head;
		   }
		   else
		       prev = temp.next;
			   temp = current.next;
		   
	   }
	   return head;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900