11package linkedlists ;
22
3- import linkedlists .RemoveDuplicates .Node ;
4-
53//Given head pointer of a linked list, sort it in ascending order using insertion sort.
64public class InsertionSortList {
75
8- static Node head ;
6+ Node head ;
97
108static class Node {
119
@@ -35,27 +33,28 @@ public static void main(String args[]){
3533list1 .head .next .next .next .next = new Node (21 );
3634list1 .head .next .next .next .next .next = new Node (14 );
3735
38- printlist (head );
36+ printlist (list1 . head );
3937System .out .println ("\n " +"test1" );
4038
41-
42- printlist (insertion_sort (head ));
39+ printlist (insertion_sort (list1 .head ));
4340}
4441private static Node insertion_sort (Node head1 ) {
4542
43+ //InsertionSortList list2 = new InsertionSortList();
4644
4745Node head2 = new Node (head1 .data );
4846head1 = head1 .next ;
47+
4948while (head1 != null ){
50- if (head1 .data < head2 .data ){
49+ if (head1 .data <= head2 .data ){
5150Node temp = new Node (head2 .data );
5251head2 = new Node (head1 .data );
5352head2 .next = temp ;
5453head1 = head1 .next ;
5554} else if (head1 .data > head2 .data ){
5655Node start = head2 ;
5756while ((head1 .data > head2 .data )&&(head2 .next !=null )){
58- // System.out.println(head1.data+","+head2.data);
57+ System .out .println (head1 .data +"," +head2 .data );
5958head2 = head2 .next ;
6059System .out .println (head1 .data +"," +head2 .data +"inside" );
6160}
@@ -73,38 +72,6 @@ private static Node insertion_sort(Node head1) {
7372
7473}
7574}
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-
10775return head2 ;
108-
10976}
11077}
0 commit comments