Skip to content

Instantly share code, notes, and snippets.

@cxtadment
Created September 2, 2015 16:39
Show Gist options
  • Select an option

  • Save cxtadment/46237da3c8c9c9814538 to your computer and use it in GitHub Desktop.

Select an option

Save cxtadment/46237da3c8c9c9814538 to your computer and use it in GitHub Desktop.
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The first node of linked list.
* @return: The head of linked list.
*/
public ListNode insertionSortList(ListNode head) {
// write your code here
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(0);
ListNode curr = head;
while (curr != null) {
ListNode pre = dummy;
while (pre.next != null && pre.next.val < curr.val) {
pre = pre.next;
}
ListNode temp = curr.next;
curr.next = pre.next;
pre.next = curr;
curr = temp;
}
return dummy.next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment