-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorting_of_LinkedList.java
86 lines (86 loc) · 2.19 KB
/
Sorting_of_LinkedList.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.Scanner;
class Node
{
int data;
Node next;
Node(int val)
{
data = val;
next = null;
}
}
public class Creation_Of_Linked_List
{
static Node add(int data)
{
Node newnode = new Node(data);
newnode.data = data;
newnode.next = null;
return newnode;
}
static Node addElements(int[] arr, Node head,int len)
{
head = add(arr[0]);
Node curr = head;
for (int index=1;index<len;index++)
{
curr.next=add(arr[index]);
curr=curr.next;
}
return head;
}
static void sortLinkedList(Node head)
{
Node current = head, index = null;
int temp;
if(head == null)
return;
else
{
while(current != null)
{
index = current.next;
while(index != null)
{
if(current.data > index.data)
{
temp = current.data;
current.data = index.data;
index.data = temp;
}
index = index.next;
}
current = current.next;
}
}
}
static void print(Node head)
{
Node curr = head;
while (curr != null)
{
System.out.print(curr.data);
System.out.print("->");
curr = curr.next;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int[] arr=new int[N];
for(int index=0;index<N;index++)
{
arr[index]=sc.nextInt();
}
Node head = null;
head = addElements(arr,head,N);
System.out.println("Linked List Before Sorting: ");
print(head);
System.out.println();
sortLinkedList(head);
System.out.println("Linked List After Sorting: ");
print(head);
sc.close();
}
}