Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 377ea5c

Browse files
author
Ram swaroop
committed
linkedlist done (tested)
1 parent c5687fc commit 377ea5c

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/me/ramswaroop/linkedlists/SingleLinkedList.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public LinkedList<E> clone() {
7373

7474
@Override
7575
public boolean contains(E item) {
76-
return getNode(item) == null;
76+
return getNode(item) != null;
7777
}
7878

7979
@Override
@@ -104,20 +104,27 @@ public E remove() {
104104

105105
@Override
106106
public E remove(int index) {
107+
isLinkedListEmpty();
108+
107109
Node<E> prevNode = getPredecessorNode(index);
110+
Node<E> delNode;
108111
if (prevNode == null) { // index = 0
112+
delNode = head;
109113
head = head.next;
110114
size--;
111-
return head.item;
115+
return delNode.item;
112116
} else {
113-
prevNode.next = prevNode.next.next;
117+
delNode = prevNode.next;
118+
prevNode.next = delNode.next;
114119
size--;
115-
return prevNode.next.item;
120+
return delNode.item;
116121
}
117122
}
118123

119124
@Override
120125
public boolean remove(E item) {
126+
isLinkedListEmpty();
127+
121128
if (!contains(item)) return false;
122129

123130
Node<E> prevNode = getPredecessorNode(item);
@@ -232,8 +239,8 @@ private void isLinkedListEmpty() {
232239
}
233240

234241
private void isIndexOutOfBounds(int index) {
235-
if (index < 0 && index > size) {
236-
throw new IndexOutOfBoundsException("Index must be less than or equal to: " + size);
242+
if (index < 0 || index > size) {
243+
throw new IndexOutOfBoundsException("Index must be less than " + size);
237244
}
238245
}
239246

0 commit comments

Comments
 (0)