-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insertion sort a linked list
43 lines (40 loc) · 1.16 KB
/
Insertion sort a linked list
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
class Solution {
public ListNode insertionSortList(ListNode head) {
ListNode curr=head;
ListNode temp=null;
while(curr!=null){
ListNode next=curr.next;
// System.out.println("curr1"+curr.val);
temp=sort(temp, curr);
curr=next;
// System.out.println("curr2"+curr.val);
// curr=curr.next;
// System.out.println("curr3"+curr);
}
return temp;
}
ListNode sort(ListNode sorted_head, ListNode curr){
if(sorted_head==null || curr.val<=sorted_head.val){
curr.next=sorted_head;
sorted_head=curr;
// printList(sorted_head);
}
else{
ListNode temp=sorted_head;
while(temp.next!=null && curr.val>temp.next.val){
temp=temp.next;
}
curr.next=temp.next;
temp.next=curr;
// printList(sorted_head);
}
return sorted_head;
}
void printList(ListNode head){
while(head!=null) {
System.out.println(head.val);
head=head.next;
}
}
}
#Status: Solved