-
Notifications
You must be signed in to change notification settings - Fork 0
/
LL.java
174 lines (159 loc) · 3.62 KB
/
LL.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
public class LL{
public static class Node{
int data;
Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
public static Node head;
public int size;
public void addFirst(int data){
Node temp=head;
Node newNode = new Node(data);
size++;
if(temp==null){
head=newNode;
return;
}
newNode.next = head;
head = newNode;
}
public void addLast(int data){
Node temp=head;
Node newNode = new Node(data);
size++;
while(temp.next!=null){
temp=temp.next;
}
temp.next = newNode;
}
public void print(){
Node temp = head;
while(temp!=null){
System.out.print(temp.data + " --> ");
temp=temp.next;
}
System.out.println("null");
}
public void add(int index,int data){
if(index == 0){
addFirst(data);
return;
}
Node temp = head;
int i = 0;
while(i<index-1){
i++;
temp = temp.next;
}
Node newNode = new Node(data);
size++;
newNode.next = temp.next;
temp.next = newNode;
}
public int removeFirst(){
if(size==0){
System.out.println("LL is empty");
}
int val = head.data;
head = head.next;
size--;
return val;
}
public int removeLast(){
if(size==0){
System.out.println("LL is empty");
}
Node temp = head;
for (int i = 0; i< size -2;i++){
temp = temp.next;
}
int val = temp.next.data;
temp.next = null;
size--;
return val;
}
public int itrSearch(int key){
Node temp = head;
int i = 0;
while(temp!= null){
if(temp.data == key){
return i;
}
temp = temp.next;
i++;
}
return -1;
}
public int helper(Node head, int key){
if(head == null){
return -1;
}
if(head.data == key){
return 0;
}
int i = helper(head.next, key);
if(i==-1){
return -1;
}
return i+1;
}
public int recSearch(int key){
return helper(head,key);
}
public void reverse(){
Node prev = null;
Node curr = head;
Node next;
while(curr!=null){
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
}
public void deletefromnthend(int n){
int sz = 0;
Node temp = head;
while (temp != null) {
sz++;
temp = temp.next;
}
if(n==sz){
head = head.next;
return;
}
int i = 1;
int f = sz-n;
Node prev = head;
while(i<f){
prev = prev.next;
i++;
}
prev.next = prev.next.next;
return;
}
public static void main(String[] args) {
LL l = new LL();
l.addFirst(2);
l.addFirst(1);
l.addLast(3);
l.addLast(4);
l.print();
l.add(4, 5);
l.print();
l.reverse();
l.print();
l.removeFirst();
l.removeLast();
l.print();
l.deletefromnthend(1);
l.print();
System.out.println(l.itrSearch(3));
System.out.println(l.recSearch(3));
System.out.println(l.size);
}
}