-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListNode.java
47 lines (40 loc) · 926 Bytes
/
LinkedListNode.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
package techQuestions;
// Erich's LinkedListNode public class for many Cracking the
// Coding Interview Questions
public class LinkedListNode {
public LinkedListNode prev, next, last;
// node will contain integer data type
public int data;
public LinkedListNode(int d, LinkedListNode n, LinkedListNode p) {
data = d;
setNext(n);
setPrevious(p);
}
public LinkedListNode(int d) {
data = d;
}
public LinkedListNode() {}
public void setNext(LinkedListNode n) {
next = n;
if (this == last) {
last = n;
}
if (n != null && n.prev != this) {
n.setPrevious(this);
}
}
public void setPrevious(LinkedListNode p) {
prev = p;
if (p != null && p.next != this) {
p.setNext(this);
}
}
public LinkedListNode clone() {
LinkedListNode next2 = null;
if (next != null) {
next2 = next.clone();
}
LinkedListNode head2 = new LinkedListNode(data, next2, null);
return head2;
}
}