Skip to content

Commit 55866a3

Browse files
committed
update doubly linked list and add code review markdown
1 parent e160c6e commit 55866a3

File tree

3 files changed

+244
-4
lines changed

3 files changed

+244
-4
lines changed

codereview/DoublyLinkedList.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
## Purpose
2+
3+
I've never implemented a `DoublyLinkedList` and thought it would be a good data structure exercise. I tried using outside
4+
resources as little as possible. I also tried to keep things as simple as possible
5+
6+
## Discussion
7+
8+
The `DoublyLinkedList` is made up of a `Node` `head`, a `Node` `tail`, and an `int` `length`. I did not genericize the
9+
API to keep things simple - the API only deals with `int` values.
10+
11+
The API is
12+
13+
* `getHead` - gets the `Node` at the `head` of the linked list in `O(1)`
14+
* `getTail` - gets the `Node` at the `tail` of the linked list in `O(1)`
15+
* `getLength` - gets the `length` of the linked list in `O(1)`
16+
* `isEmpty` - convenience method; returns `true` if `length` is `0` and `false` otherwise in `O(1)`
17+
* `insertAtHead` - adds the input value at the `head` of the linked list in `O(1)`
18+
* `insertAtTail` - adds the input value at the `tail` of the linked list in `O(1)`
19+
* `insertAt` - adds the input value at some index in the linked list in `O(n)`
20+
* `removeFirstOccurrence` - removes the first occurrence of the input value (if it exists in list) in `O(n)`
21+
* `removeAt` - removes the node at the specified index in the linked list in `O(n)`
22+
* `removeHead` - removes the node at the `head` of the linked list in `O(1)`
23+
* `removeTail` - removes the node at the `tail` of the linked list in `O(1)`
24+
25+
Things I can improve:
26+
27+
* When inserting at / removing at a particular index, I could speed up execution time by picking to start
28+
at the beginning or end of the list based on which the index is closest to
29+
* I tried adding a bunch of test cases, but I could have easily missed a case
30+
* Is my API sane / reasonable / did I miss implementing any obvious methods?
31+
32+
## Implementation
33+
34+
<!-- language:lang-java --!>
35+
36+
public class DoublyLinkedList {
37+
public static class Node {
38+
private Node previous;
39+
private Node next;
40+
private int value;
41+
42+
public Node(int value) {
43+
this.value = value;
44+
}
45+
46+
public Node getPrevious() {
47+
return previous;
48+
}
49+
50+
public Node getNext() {
51+
return next;
52+
}
53+
54+
public int getValue() {
55+
return value;
56+
}
57+
}
58+
59+
private Node head;
60+
private Node tail;
61+
private int length;
62+
63+
public DoublyLinkedList() {
64+
}
65+
66+
public Node getHead() {
67+
return head;
68+
}
69+
70+
public Node getTail() {
71+
return tail;
72+
}
73+
74+
public int getLength() {
75+
return length;
76+
}
77+
78+
public boolean isEmpty() {
79+
return length == 0;
80+
}
81+
82+
public void insertAtHead(int value) {
83+
insertNode(null, new Node(value), head);
84+
}
85+
86+
public void insertAtTail(int value) {
87+
insertNode(tail, new Node(value), null);
88+
}
89+
90+
public void insertAt(int value, int index) {
91+
if (index > length || index < 0) {
92+
throw new IndexOutOfBoundsException("index of " + index + " is out of bounds");
93+
}
94+
95+
Node node = new Node(value);
96+
Node previousNode = null;
97+
Node currentNodeAtIndex = head;
98+
for (int i = 0; i < index; i++) {
99+
previousNode = currentNodeAtIndex;
100+
currentNodeAtIndex = currentNodeAtIndex.next;
101+
}
102+
insertNode(previousNode, node, currentNodeAtIndex);
103+
}
104+
105+
public void removeFirstOccurrence(int value) {
106+
Node currentNode = head;
107+
while (currentNode != null) {
108+
if (currentNode.value == value) {
109+
removeNode(currentNode);
110+
return;
111+
}
112+
currentNode = currentNode.next;
113+
}
114+
}
115+
116+
public void removeAt(int index) {
117+
if (index >= length || index < 0) {
118+
throw new IndexOutOfBoundsException("index of " + index + " is out of bounds");
119+
}
120+
121+
Node currentNodeAtIndex = head;
122+
for (int i = 0; i < index; i++) {
123+
currentNodeAtIndex = currentNodeAtIndex.next;
124+
}
125+
126+
removeNode(currentNodeAtIndex);
127+
}
128+
129+
public void removeHead() {
130+
removeNode(head);
131+
}
132+
133+
public void removeTail() {
134+
removeNode(tail);
135+
}
136+
137+
private void removeNode(Node node) {
138+
if (node.previous == null) {
139+
head = node.next;
140+
}
141+
142+
if (node.next == null) {
143+
tail = node.previous;
144+
}
145+
146+
if (node.previous != null) {
147+
node.previous.next = node.next;
148+
}
149+
150+
if (node.next != null) {
151+
node.next.previous = node.previous;
152+
}
153+
154+
length--;
155+
}
156+
157+
private void insertNode(Node nodeBefore, Node node, Node nodeAfter) {
158+
node.next = nodeAfter;
159+
node.previous = nodeBefore;
160+
161+
if (nodeBefore == null) {
162+
head = node;
163+
}
164+
165+
if (nodeAfter == null) {
166+
tail = node;
167+
}
168+
169+
if (nodeBefore != null) {
170+
nodeBefore.next = node;
171+
}
172+
173+
if (nodeAfter != null) {
174+
nodeAfter.previous = node;
175+
}
176+
177+
length++;
178+
}
179+
}

src/main/java/problems/impl/DoublyLinkedList.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public int getValue() {
2525

2626
private Node head;
2727
private Node tail;
28-
private int length = 0;
28+
private int length;
2929

3030
public DoublyLinkedList() {
3131
}
@@ -93,6 +93,14 @@ public void removeAt(int index) {
9393
removeNode(currentNodeAtIndex);
9494
}
9595

96+
public void removeHead() {
97+
removeNode(head);
98+
}
99+
100+
public void removeTail() {
101+
removeNode(tail);
102+
}
103+
96104
private void removeNode(Node node) {
97105
if (node.previous == null) {
98106
head = node.next;
@@ -102,15 +110,21 @@ private void removeNode(Node node) {
102110
tail = node.previous;
103111
}
104112

105-
if (node.previous != null && node.next != null) {
113+
if (node.previous != null) {
106114
node.previous.next = node.next;
115+
}
116+
117+
if (node.next != null) {
107118
node.next.previous = node.previous;
108119
}
109120

110121
length--;
111122
}
112123

113124
private void insertNode(Node nodeBefore, Node node, Node nodeAfter) {
125+
node.next = nodeAfter;
126+
node.previous = nodeBefore;
127+
114128
if (nodeBefore == null) {
115129
head = node;
116130
}
@@ -119,8 +133,13 @@ private void insertNode(Node nodeBefore, Node node, Node nodeAfter) {
119133
tail = node;
120134
}
121135

122-
node.next = nodeAfter;
123-
node.previous = nodeBefore;
136+
if (nodeBefore != null) {
137+
nodeBefore.next = node;
138+
}
139+
140+
if (nodeAfter != null) {
141+
nodeAfter.previous = node;
142+
}
124143

125144
length++;
126145
}

src/test/java/problems/impl/DoublyLinkedListTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,46 @@ public void removeAtZeroForSingleElementList() {
188188
assertNull(list.getHead());
189189
assertEquals(list.getHead(), list.getTail());
190190
}
191+
192+
@Test
193+
public void removeHeadForSingleElementList() {
194+
DoublyLinkedList list = new DoublyLinkedList();
195+
list.insertAtHead(1);
196+
list.removeHead();
197+
assertEquals(0, list.getLength());
198+
assertNull(list.getHead());
199+
assertEquals(list.getHead(), list.getTail());
200+
}
201+
202+
@Test
203+
public void removeHeadForTwoElementList() {
204+
DoublyLinkedList list = new DoublyLinkedList();
205+
list.insertAtHead(2);
206+
list.insertAtHead(1);
207+
list.removeHead();
208+
assertEquals(1, list.getLength());
209+
assertEquals(2, list.getHead().getValue());
210+
assertEquals(list.getHead(), list.getTail());
211+
}
212+
213+
@Test
214+
public void removeTailForSingleElementList() {
215+
DoublyLinkedList list = new DoublyLinkedList();
216+
list.insertAtHead(1);
217+
list.removeTail();
218+
assertEquals(0, list.getLength());
219+
assertNull(list.getHead());
220+
assertEquals(list.getHead(), list.getTail());
221+
}
222+
223+
@Test
224+
public void removeTailForTwoElementList() {
225+
DoublyLinkedList list = new DoublyLinkedList();
226+
list.insertAtHead(2);
227+
list.insertAtHead(1);
228+
list.removeTail();
229+
assertEquals(1, list.getLength());
230+
assertEquals(1, list.getHead().getValue());
231+
assertEquals(list.getHead(), list.getTail());
232+
}
191233
}

0 commit comments

Comments
 (0)