1
+ package linkedlists ;
2
+
3
+ import linkedlists .RemoveDuplicates .Node ;
4
+
5
+ //Given head pointer of a linked list, sort it in ascending order using insertion sort.
6
+ public class InsertionSortList {
7
+
8
+ static Node head ;
9
+
10
+ static class Node {
11
+
12
+ public int data ;
13
+ public Node next ;
14
+
15
+ Node (int a ){
16
+ this .data = a ;
17
+ this .next = null ;
18
+ }
19
+ }
20
+
21
+ static void printlist (Node start ){
22
+
23
+ while (start != null ){
24
+ System .out .print (start .data +" " );
25
+ start = start .next ;
26
+ }
27
+ }
28
+ public static void main (String args []){
29
+
30
+ InsertionSortList list1 = new InsertionSortList ();
31
+ list1 .head = new Node (17 );
32
+ list1 .head .next = new Node (7 );
33
+ list1 .head .next .next = new Node (41 );
34
+ list1 .head .next .next .next = new Node (28 );
35
+ list1 .head .next .next .next .next = new Node (21 );
36
+ list1 .head .next .next .next .next .next = new Node (14 );
37
+
38
+ printlist (head );
39
+ System .out .println ("\n " +"test1" );
40
+
41
+
42
+ printlist (insertion_sort (head ));
43
+ }
44
+ private static Node insertion_sort (Node head1 ) {
45
+
46
+
47
+ Node head2 = new Node (head1 .data );
48
+ head1 = head1 .next ;
49
+ while (head1 != null ){
50
+ if (head1 .data < head2 .data ){
51
+ Node temp = new Node (head2 .data );
52
+ head2 = new Node (head1 .data );
53
+ head2 .next = temp ;
54
+ head1 = head1 .next ;
55
+ } else if (head1 .data > head2 .data ){
56
+ Node start = head2 ;
57
+ while ((head1 .data > head2 .data )&&(head2 .next !=null )){
58
+ //System.out.println(head1.data+","+head2.data);
59
+ head2 = head2 .next ;
60
+ System .out .println (head1 .data +"," +head2 .data +"inside" );
61
+ }
62
+
63
+ Node temp = new Node (head1 .data );
64
+ head2 .next = temp ;
65
+ head2 = start ;
66
+
67
+
68
+ System .out .println ("sorted list as of now" );
69
+ printlist (head2 );
70
+ System .out .println ("" );
71
+ System .out .println (head1 .data +"," +head2 .data +"outside" );
72
+ head1 = head1 .next ;
73
+
74
+ }
75
+ }
76
+
77
+
78
+ // //head2.data = 0;
79
+ // System.out.println("test2");
80
+ // while(head1 != null){
81
+ // System.out.println("testx");
82
+ // if (head2.next == null){
83
+ // System.out.println("test3");
84
+ // head2 = new Node(head1.data);
85
+ // System.out.println(head1.data);
86
+ // head1 = head1.next;
87
+ // } else if(head1.data < head2.data){
88
+ // System.out.println("test4");
89
+ // Node temp = new Node(head2.data);
90
+ // head2 = new Node(head1.data);
91
+ // head2.next = temp;
92
+ // head1 = head1.next;
93
+ // } else if(head1.data > head2.data){
94
+ // System.out.println("test4");
95
+ // Node start = head2;
96
+ // while(head1.data>head2.data){
97
+ // head2 = head2.next;
98
+ // }
99
+ // Node temp = new Node(head2.data);
100
+ // head2 = new Node(head1.data);
101
+ // head2.next = temp;
102
+ // head1 = head1.next;
103
+ // head2 = start;
104
+ // }
105
+ // }
106
+
107
+ return head2 ;
108
+
109
+ }
110
+ }
0 commit comments