public class Insertion_Sort_List {
public class ListNode{
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public ListNode insertionSortList(ListNode head) {
ListNode dummy = new ListNode(0);
while (head != null) {
ListNode node = dummy;
while (node.next != null && node.next.val < head.val) {
node = node.next;
}
ListNode temp = head.next;
head.next = node.next;
node.next = head;
head = temp;
}
return dummy.next;
}
}
Saturday, February 15, 2014
Insertion Sort List
Insertion Sort List
Sort a linked list using insertion sort.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment