-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_list.java
More file actions
239 lines (233 loc) · 5.73 KB
/
Linked_list.java
File metadata and controls
239 lines (233 loc) · 5.73 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import java.util.Stack;
public class Linked_list {
public static class Node{
private int value;
private Node next;
public Node(int value) {
this.value =value;
}
}
private Node head;
private int size;
private Node tail;
public Linked_list() {
this.size = 0;
}
public void insertfirst(int value){
Node node=new Node(value);
node.next=head;
head=node;
size++;
System.out.println("element inserted "+head.value);
if(tail==null)
tail=head;
}
public void insertlast(int value){
Node node=new Node(value);
if(size==0)
{
insertfirst(value);
return;
}
Node temp;
temp=head;
while (temp.next!=null)
temp=temp.next;
temp.next=node;
node.next=null;
tail=node;
size++;
System.out.println("element inserted "+tail.value);
}
public void insertmid(int value,int index){
if (head==null || index==0)
insertfirst(value);
// else if (index==size-1)
// insertlast(value);
else {
Node node=new Node(value);
Node node1=head;
Node node2=head;
node2=node2.next;
while (index-1>0){
node1=node1.next;
node2=node2.next;
index--;
}
node1.next=node;
node.next=node2;
System.out.println("element inserted "+node.value);
size++;
}
}
public void deletefirst(){
if (head==null)
return;
Node temp=head;
head=head.next;
size--;
System.out.println("element deleted "+temp.value);
}
public void deletelast(){
if (head==null)
return;
else if (size==1) {
head = null;
size--;
}
Node temp=head;
while (temp.next.next!=null)
temp=temp.next;
temp.next=null;
tail=temp;
size--;
System.out.println("element deleted "+tail.value);
}
public void deletemid(int index){
if (head==null)
return;
else if (size-1==index)
deletelast();
else {
Node node1=head;
Node node=node1.next;
Node node2=node.next;
while (index-1>0){
node1=node1.next;
node=node.next;
node2=node2.next;
index--;
}
node1.next=node2;
size--;
System.out.println("element deleted "+node.value);
}
}
public void reverse()
{
reverse(head);
} //using Recursion
public void reverse(Node node)
{
if(node==tail)
{
head=tail;
return;
}
reverse(node.next);
tail.next=node;
tail=node;
tail.next=null;
}
public void reverse2(){ //using stack
Node temp=head;
Stack<Integer> s=new Stack<>();
while (temp!=null){
s.push(temp.value);
temp=temp.next;
size--;
}
System.out.println(s);
head=null;
while (!s.empty()){
int a=s.pop();
insertlast(a);
}
}
public void swap(int a ,int b) //by value
{
Node temp1=head,temp2=head,temp=head;
while (temp!=null)
{
if(temp1.value==a)
break;
temp=temp.next;
temp1=temp1.next;
}
temp=head;
while(temp!=null)
{
if (temp2.value==b)
break;
temp=temp.next;
temp2=temp2.next;
}
temp1.value=b;
temp2.value=a;
}
public boolean pallindrome(Linked_list l)
{
Linked_list l1=new Linked_list();
Node temp=l.head;
while(temp!=null)
{
l1.insertlast(temp.value);
temp=temp.next;
}
l1.reverse();
temp=l.head;
Node temp2=l1.head;
while (temp!=null )
{
if(temp.value!=temp2.value)
return false;
temp=temp.next;
temp2=temp2.next;
}
return true;
}
public void ithFromLast(int k)
{
Node temp1=head,temp2=head;
for (int i = 0; i <k ; i++) {
temp1= temp1.next;
}
while(temp1!=null)
{
temp1=temp1.next;
temp2=temp2.next;
}
System.out.println("ith element is "+temp2.value);
}
public void kthreverse(int i,Linked_list l) {
Stack<Integer> s1 = new Stack<>();
Linked_list l1 = new Linked_list();
int j = i;
Node temp = l.head;
while (temp != null) {
while (temp != null && j > 0) {
s1.push(temp.value);
temp = temp.next;
j--;
}
while (!s1.isEmpty()) {
int a = s1.pop();
l1.insertlast(a);
}
j = i;
}
l1.display();
}
public void display()
{
Node node=head;
System.out.println("the list is");
while(node!=null)
{
System.out.print(node.value+" -> ");
node=node.next;
}
System.out.println("null");
}
public static void main(String[] args) {
Linked_list l=new Linked_list();
Linked_list l1=new Linked_list();
l.insertfirst(2);
l.insertlast(3);
l.insertlast(5);
l.insertlast(6);
l.insertlast(7);
l.display();
l.kthreverse(2,l);
}
}