# Time Limit Exceeded?

• Not sure why it exceeds, here is my code.
I am little confused because it's normal for insertion sort take O(n^2) time to complete sorting.

`````` public ListNode insertionSortList(ListNode head) {
ListNode resTail = null;
//if it's the first node, we can set resHead and resTail
resTail = curr;
resTail.next = null;
// if the current node is smaller than head, we insert before the resHead
// if the current node is larger than tail, we insert after the resTail
} else if(curr.val >= resTail.val){
resTail.next = curr;
resTail = curr;
resTail.next = null;
// otherwise, when the current node's value is in the middle,
// we should go through the result list to find the right insertion position
} else {
while(findPos != null){
if(findPos.next == null || curr.val < findPos.next.val){
curr.next = findPos.next;
findPos.next = curr;
break;
}
findPos = findPos.next;
}
}
}
}``````

• Could you please update your post with explanation of algorithm and comments in code? People barely help on raw code. Thanks.

• thanks for reminder, i updated the comments

• I go through your code, found nothing wrong. Then I submit it to the OJ, it passed anyway.

However, I thought the last part of your code could be neater, like this:

``````        ListNode findPos = resHead;
while(curr.val > findPos.next.val){
findPos = findPos.next;
}
curr.next = findPos.next;
findPos.next = curr;
``````