-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesignLinkedList.java
84 lines (71 loc) · 2.36 KB
/
designLinkedList.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
class MyLinkedList {
class Node{
public int val;
public Node next;
public Node(int val){
this.val = val;
}
}
private Node headNode;
public MyLinkedList() {}
public int get(int index) {
return getNodeIndex(index).val;
}
public Node getNodeIndex(int index){
Node iteratorNode = headNode;
int index2 = 0;
if(iteratorNode == null){return null;}
while(index2<=index){
iteratorNode = iteratorNode.next;
System.out.println("GET: at index "+ index2 + " : " + iteratorNode.val );
if(iteratorNode == null){
return null;
}
index2 = index2 + 1;
}
return iteratorNode;
}
public void addAtHead(int val) {
Node cur = new Node(val);
cur.next = headNode;
headNode = cur;
System.out.println("ADD HEAD: added atHead : " + val );
}
public void addAtTail(int val) {
Node cur = new Node(val);
Node lastNode = getTail();
lastNode.next = cur;
System.out.println("ADD TAIL: added at tail : " + val );
}
public Node getTail(){
Node iteratorNode = headNode;
while(iteratorNode.next!=null){iteratorNode=iteratorNode.next;}
return iteratorNode;
}
public void addAtIndex(int index, int val) {
Node cur = new Node(val);
Node prev = getNodeIndex(index-1);
if(prev == null){return;}
if(prev.next==null){addAtTail(val);}
cur.next = prev.next;
prev.next = cur;
System.out.println("ADD INDEX: added at index "+ index + " : " + val );
}
public void deleteAtIndex(int index) {
Node cur = getNodeIndex(index-1);
if(cur == null || cur.next == null){return;}
if(cur.next.next == null){cur.next=null;}
System.out.println("DELETE: deleted the index : " + index + " with the value " + cur.next.val);
cur.next = cur.next.next;
System.out.println("DELETE: the next element is now : " + cur.next.val );
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/