-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.java
234 lines (184 loc) · 3.66 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
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
package LinkedList;
public class LinkedList {
Node head;
public static void main(String[] args) {
LinkedList l=new LinkedList();
Node second=new Node(2);
Node third=new Node(3);
Node five=new Node(5);
l.head=new Node(1);
l.head.next=second;
second.next=third;
l.push(4);
//l.insertAfter(third,five);
l.append(6);
l.append(7);
//l.deleteNode(1);
//l.deleteNodePos(1);
l.printList();
// l.lengthLinkList();
int len=l.recursiveLength(l.head);
//l.swapNode(1,3);
System.out.println();
//l.printNthNode(0);
//l.printNthNodeFromBack(5);
l.middleElement();
/*System.out.println("len is:::"+len);
System.out.println(l.Search(0));
System.out.println(l.IterativeSearch(l.head,3));*/
}
private void middleElement() {
Node n=head,n1=head;
while(n!=null && n.next!=null){
n=n.next.next;
n1=n1.next;
}
System.out.println(n1.data);
}
private void printNthNodeFromBack(int pos) {
Node n=head,n1=head;
int len=0;
while(n1!=null){
len++;
n1=n1.next;
}
for(int i=0;i<len-pos-1;i++){
n=n.next;
}
System.out.println(n.data);
}
private void printNthNode(int k) {
Node n=head;
for(int i=0;i<k;i++){
n=n.next;
}
System.out.println(n.data);
}
private void swapNode(int x, int y) {
if(x==y)
return;
Node currX=head,currY=head;
Node prevX=null,prevY=null;
while(currX!=null && currY!=null){
if(currX.data==x && currY.data==y)
return;
while(currX.data!=x){
prevX=currX;
currX=currX.next;
}
while(currY.data!=y){
prevY=currY;
currY=currY.next;
}
if(prevX!=null){
prevX.next=currY;
}
else
head=currY;
if(prevY!=null){
prevY.next=currX;
}
else
head=currX;
Node temp=currY.next;
currY.next=currX.next;
currX.next=temp;
}
}
private boolean IterativeSearch(Node n, int i) {
if(n!=null)
return (n.data==i)?true:IterativeSearch(n.next,i);
else
return false;
}
private boolean Search(int i) {
Node n=head;
while(n!=null){
if(n.data==i)
return true;
n=n.next;
}
return false;
}
private int recursiveLength(Node n) {
if (n==null){
return 0;
}
else
return 1+recursiveLength(n.next);
}
private void lengthLinkList() {
int count=0;
Node n=head;
while(n!=null){
n=n.next;
count++;
}
System.out.println();
System.out.println("length::::"+count);
}
private void deleteNodePos(int pos) {
Node n=head;
if(pos==0)
head=n.next;
else{
for(int i=0;i<pos-1;i++){
n=n.next;
}
n.next=n.next.next;
}
}
private void deleteNode(int i) {
Node temp=head;
Node prev=null;
while(temp!=null && temp.data==i)
{
head=temp.next;
}
while(temp!=null && temp.data!=i){
prev=temp;
temp=temp.next;
}
if(temp==null)
System.out.println("Key Not Found");
prev.next=temp.next;
}
private void insertAfter(Node Prev, Node NewNode) {
Node n=head;
if(Prev==null)
System.out.println("Previous node cant be null");
while(n.data!=Prev.data){
n=n.next;
}
NewNode.next=Prev.next;
Prev.next=NewNode;
}
private void append(int i) {
Node temp=new Node(i);
Node n=head;
while(n.next!=null)
n=n.next;
n.next=temp;
}
private void push(int i) {
Node n=new Node(4);
n.next=head;
head=n;
}
private void printList() {
Node n=head;
while(n!=null){
System.out.print(n.data+"----->");
n=n.next;
}
}
static class Node{
int data;
Node next;
Node(int data)
{
this.data=data;
next=null;
}
}
}