-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListPrbs.java
More file actions
206 lines (164 loc) · 4.22 KB
/
Copy pathLinkedListPrbs.java
File metadata and controls
206 lines (164 loc) · 4.22 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
class Node {
int data;
Node next;
Node(int data, Node next) {
this.data = data;
this.next = next;
}
Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedListSolutions {
public Node convertArrayToLL(int[] arr) {
Node head = new Node(arr[0]);
Node mover = head;
Node temp;
for (int idx = 1; idx < arr.length; idx++) {
temp = new Node(arr[idx]);
mover.next = temp;
mover = temp;
}
return head;
}
public void printLL(Node head) {
Node temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
public int findLength(Node head) {
int count = 0;
Node temp = head;
while (temp != null) {
count += 1;
temp = temp.next;
}
return count;
}
public boolean findElement(Node head, int ele) {
Node temp = head;
while (temp != null) {
if (temp.data == ele) {
return true;
}
temp = temp.next;
}
return false;
}
public Node deleteHead(Node head) {
if (head == null)
return head;
head = head.next;
return head;
}
public Node deleteTail(Node head) {
if (head == null || head.next == null)
return null;
Node temp = head;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
return head;
}
public Node deleteAtK(Node head, int k) {
if (head == null)
return head;
if (k == 1) {
head = head.next;
return head;
}
Node temp = head, prev = null;
int count = 1;
while (temp != null) {
if (count == k) {
prev.next = temp.next;
break;
}
prev = temp;
temp = temp.next;
count++;
}
return head;
}
public Node deleteValue(Node head, int val) {
if (head == null)
return head;
if (head.data == val) {
head = head.next;
return head;
}
Node temp = head, prev = null;
while (temp != null) {
if (temp.data == val) {
prev.next = temp.next;
break;
}
prev = temp;
temp = temp.next;
}
return head;
}
public Node insertAtTail(Node head, int val) {
if (head == null) {
return new Node(val);
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = new Node(val);
return head;
}
public Node insertAtK(Node head, int val, int k) {
if (head == null) {
if (k == 1)
return new Node(val);
else
return null;
}
if (k == 1) {
return new Node(val, head);
}
Node temp = head;
int count = 1;
while (temp != null) {
if (count + 1 == k) {
temp.next = new Node(val, temp.next);
break;
}
temp = temp.next;
count++;
}
return head;
}
public Node insertBeforeVal(Node head, int val, int k) {
if (head == null)
return null;
if (head.data == k) {
return new Node(val, head);
}
Node temp = head;
while (temp.next != null) {
if (temp.next.data == k) {
temp.next = new Node(val, temp.next);
break;
}
temp = temp.next;
}
return head;
}
}
public class LinkedListPrbs {
public static void main(String[] args) {
LinkedListSolutions solution = new LinkedListSolutions();
int[] arr = new int[] { 3, 4, 9, 8, 5 };
Node head = solution.convertArrayToLL(arr);
// Node newHead = new Node(23, head);
head = solution.insertBeforeVal(head, 100, 9);
solution.printLL(head);
}
}