-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
140 lines (125 loc) · 3.07 KB
/
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class LL{
private Node head;
private Node tail;
private int size;
private class Node{
private int value;
private Node next;
public Node(int value) {
this.value = value;
}
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
public LL(){
this.size = 0;
}
public void insertfirst(int value){
Node node = new Node(value);
node.next = head;
head = node;
if (tail==null){
tail = head;
}
size +=1;
}
public void insertlast(int value){
Node node = new Node(value);
tail.next = node;
tail = node;
size +=1;
}
public int deletelast(){
if(size<=1){
return deletefirst();
}
Node second_last = get(size - 2);
int val = tail.value;
tail = second_last;
tail.next = null;
return val;
}
public Node find(int value){
Node node = head;
while(node!=null){
if(node.value==value){
return node;
}
node = node.next;
}
return null;
}
public Node get(int index){
Node node = head;
for(int i=0; i< index;i++){
node = node.next;
}
return node;
}
public void insert(int value, int index){
if(index == 0){
insertfirst(value);
return;
}
if(index == size){
insertlast(value);
return;
}
Node temp = head;
for(int i =1; i< index ; i++){
temp=temp.next;
}
Node node = new Node(value,temp.next);
temp.next = node;
}
public int deletefirst(){
int val = head.value;
head = head.next;
if(head == null){
tail = null;
}
return val;
}
public int delete(int index){
if(index == 0){
return deletefirst();
}
if(index == size-1){
return deletelast();
}
Node prev = get(index-1);
int val = prev.next.value;
prev.next = prev.next.next;
return val;
}
public void display(){
Node temp = head;
while (temp != null){
System.out.print(temp.value + " -> ");
temp = temp.next;
}
System.out.println("END");
}
}
public class LinkedList{
public static void main(String[] args) {
LL list = new LL();
list.insertfirst(0);
list.insertfirst(1);
list.insertfirst(2);
list.insertfirst(3);
list.insertlast(-1);
list.display();
list.insert(23, 3);
list.display();
System.out.println(list.deletefirst());
list.display();
System.out.println(list.deletelast());
list.display();
System.out.println(list.delete(2));
list.display();
System.out.println(list.find(1));
}
}