-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertionSortList.java
72 lines (66 loc) · 1.55 KB
/
InsertionSortList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Sort a linked list using insertion sort.
*/
import java.io.*;
import java.util.*;
public class InsertionSortList {
public static class ListNode {
int val;
ListNode next;
public ListNode(int x) { val = x; }
}
public static ListNode insertionSortList(ListNode head) {
if(head == null) return null;
ListNode res = new ListNode(Integer.MIN_VALUE);
res.next = head;
ListNode cur = res;
while(cur!= null && cur.next != null){
if(cur.next.val < cur.val){
ListNode temp = cur.next;
cur.next = cur.next.next;
findNode(res, temp);
}else{
cur = cur.next;
}
}
return res.next;
}
public static void findNode(ListNode res, ListNode cur){
while(res.next!= null && cur.val > res.next.val){
res = res.next;
}
ListNode t = res.next;
res.next = cur;
cur.next = t;
return;
}
public static void main(String[] args) {
ListNode head = new ListNode(3);
ListNode node2 = new ListNode(1);
ListNode node3 = new ListNode(7);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(9);
ListNode node6 = new ListNode(5);
head.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = null;
System.out.print( "Before: ");
ListNode curr = head;
while(curr != null){
System.out.print( curr.val + " ");
curr = curr.next;
}
System.out.println("");
System.out.print("After: ");
ListNode newHead = insertionSortList(head);
curr = newHead;
while(curr != null){
System.out.print( curr.val + " ");
curr = curr.next;
}
return;
}
}