Skip to content

Instantly share code, notes, and snippets.

@chintanparikh
Created January 18, 2013 17:32
Show Gist options
  • Save chintanparikh/4566387 to your computer and use it in GitHub Desktop.
Save chintanparikh/4566387 to your computer and use it in GitHub Desktop.
@Override
public E remove(int index) throws IndexOutOfBoundsException{
if (index >= size || index < 0) throw new IndexOutOfBoundsException();
Node<E> prev = null, temp = head;
E data;
int a = 0;
// Set temp to the node to be deleted
while(a != index)
{
temp = temp.getNext();
prev = (prev == null) ? head : prev.getNext();
a++;
}
data = temp.getData();
// If it's the head node, update head
if (index == 0)
{
head = temp.getNext();
tail.setNext(head);
}
else
{
prev.setNext(temp.getNext());
if (index == size)
{
tail = prev;
}
}
size--;
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment