-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.java
More file actions
46 lines (40 loc) · 1.28 KB
/
1.java
File metadata and controls
46 lines (40 loc) · 1.28 KB
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
import java.util.*;
class Main {
static class Node {
int data;
Node prev, next;
Node(int data) { this.data = data; }
}
static class DoublyLinkedList {
Node head;
void append(int data) {
Node newNode = new Node(data);
if (head == null) { head = newNode; return; }
Node temp = head;
while (temp.next != null) temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
}
void sort() {
List<Integer> vals = new ArrayList<>();
for (Node t = head; t != null; t = t.next) vals.add(t.data);
Collections.sort(vals);
head = null;
for (int v : vals) append(v);
}
void print() {
for (Node t = head; t != null; t = t.next) {
System.out.print(t.data);
if (t.next != null) System.out.print(" <-> ");
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
DoublyLinkedList dll = new DoublyLinkedList();
for (int i = 0; i < n; i++) dll.append(sc.nextInt());
dll.sort();
dll.print();
}
}