# Why is using 2 while loops so much slower?

• So my solution is as follows:

'''

``````    ListNode first = head;

while (second != null && second.val == head.val) {
second = second.next;
}
}
return first;

}
``````

'''

Why is much solution 2ms compared to other solutions? Isn't the time complexity O(n)?

• Hello!
Yes, it has to be O(n), even if you use two `while` loops.

But, why use two loops, when you could do the same with one? I've provided the solution bellow with one `while` loop, which is based upon the same idea as yours with two `while` loops.

Look here:

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
while(node!=null){
if(node.val==previous.val)
node=previous.next=node.next;
else
previous=previous.next;
}
}
``````

Also, I couldn't restrained myself from writing recursive solution.
So, recursive approach takes the same time as solutions above - O(n)
Here is the code:

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {