-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSingleLinkedList.java
189 lines (163 loc) · 4.26 KB
/
SingleLinkedList.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
package Lists;
import java.util.Collection;
public class SingleLinkedList<T> implements Cloneable {
private Node root, tail;
private int size = 0;
private class Node<T> {
T val;
Node next;
public Node(T val, Node next) {
this.val = val;
this.next = next;
}
}
public boolean add(T val) {
if (root == null) {
root = new Node(val, null);
tail = root;
size++;
return tail.val.equals(val);
}
tail.next = new Node(val, null);
tail = tail.next;
size++;
return tail.val.equals(val);
}
public void add(int index, T val) throws Exception {
Node temp = root;
if (index > size) {
throw new Exception("No such index found exception");
}
if (index == size) {
add(val);
return;
}
if (index == 0) {
Node node = new Node(val, root);
root = node;
size++;
return;
}
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
Node node = new Node(val, temp.next);
temp.next = node;
size++;
}
public boolean contains(T val) {
Node temp = root;
while (temp != null) {
if (temp.val.equals(val)) {
return true;
}
temp = temp.next;
}
return false;
}
public boolean addAll(Collection<? extends T> c) {
int prevSize = size();
c.forEach(elem -> add(elem));
return size() - prevSize == c.size();
}
public int size() {
return size;
}
public boolean isEmpty() {
return size() == 0;
}
public void clear() {
size = 0;
root = null;
tail = root;
}
public T get(int index) throws Exception {
Node temp = root;
if (index >= size) {
throw new Exception("No such index found exception");
}
for (int i = 0; i < index; i++) {
temp = temp.next;
}
return (T) temp.val;
}
public T set(int index, T val) throws Exception {
Node temp = root;
if (index > size - 1) {
throw new Exception("No such index found exception");
}
for (int i = 0; i < index; i++) {
temp = temp.next;
}
temp.val = val;
return (T) temp.val;
}
public T remove(int index) throws Exception {
if (index > size - 1) {
throw new RuntimeException("No such index found exception");
}
Node temp = root;
if (index == 0) {
T ret = (T) root.val;
root = root.next;
size--;
return ret;
}
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
T ret = (T) temp.next.val;
temp.next = temp.next.next;
size--;
return ret;
}
@Override
public String toString() {
StringBuilder ret = new StringBuilder("[");
Node temp = root;
if (temp == null) {
ret.append("]");
return ret.toString();
}
while (temp.next != null) {
ret.append(temp.val + ",");
temp = temp.next;
}
ret.append(temp.val + "]");
return ret.toString();
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Object[] toArray() {
Node temp = root;
Object[] arr = new Object[size];
int i = 0;
while (temp != null) {
arr[i++] = temp.val;
temp = temp.next;
}
return arr;
}
public T pop() throws Exception {
return remove(size - 1);
}
public T remove() throws Exception {
//removes head as per Java Docs
return remove(0);
}
public T removeFirst() throws Exception {
return remove();
}
public T removeLast() throws Exception {
return pop();
}
public T peek() {
if (root != null) {
return (T) root.val;
} else {
throw new RuntimeException("Index Out of Bound Exception");
}
}
}