This is the first blog on linked lists that contains implementations in Java and introduces basic
operation such as add, get and delete. You can also read about problems and their solutions on linked lists
on LinkedList Common Problems and Solutions post.
Linked List is a sequence of data that are connected by links. Each list item is commonly referred as Node.
Every node stores certain data and a link to the next element. On the other hand, Linked lists can also have
two links (previous and next) to optimize some operations. They are called doubly linked lists.
Here is a simple illastration of singly linked lists.
and doubly linked list
Here is simple implementation of singly linked list in Java
classLinkedList<E>{privateNodehead;// the first elementprivateNodetail;// the last elementintsize;publicvoidadd(Eelement){// tail and head are the same node when list has only one itemif(head==null){head=tail=newNode(element);}else{//assign new node to next of tail and make tail the last elementtail.next=newNode(element);tail=tail.next;}size++;}publicEget(intindex){if(index>=size&&index<0)// check boundariesthrownewNoSuchElementException();// find element at given indexNodenode=head;for(inti=0;i<index;i++)node=node.next;returnnode.elem;}publicbooleandelete(Eelement){Nodetemp=newNode(null,head);// dummy element to avoid if checksNodedummy=temp;while(temp.next!=null){if(temp.next.elem.equals(element)){// if found node is last node of the list, need to change tailif(temp.next==tail)tail=temp;temp.next=temp.next.next;// if found node is first node if the list, need to change headhead=dummy.next;size--;returntrue;}temp=temp.next;}returnfalse;}// wrapper class for list elementsprivateclassNode{Eelem;Nodenext;publicNode(Eelem){this.elem=elem;}publicNode(Eel,Nodenode){this.elem=el;this.next=node;}}}
You can add addFirst and getFirst, addLast and getLast methods by changing references of
head and tail respectively. They are easy and constant operations.
And here is implementaion of doubly linked list. Every node stores element data and two links: prev and next.
classDoublyLinkedList<E>{privateNodehead;// the first elementprivateNodetail;// the last elementintsize;publicvoidadd(Eelement){if(element==null)thrownewAssertionError();// tail and head are the same node when list has only one itemif(head==null){head=tail=newNode(element);}else{//assign new node to next of tail and make tail the last elementtail.next=newNode(element,tail,null);tail=tail.next;}size++;}publicbooleanisEmpty(){returnsize==0;}publicintgetSize(){returnsize;}publicEget(intindex){if(index>=size&&index<0)// check boundariesthrownewNoSuchElementException();// find element at given indexNodenode=head;for(inti=0;i<index;i++)node=node.next;returnnode.elem;}publicbooleandelete(Eelement){Nodetemp=head;while(temp!=null&&!temp.elem.equals(element)){temp=temp.next;}// no node with given element is foundif(temp==null)returnfalse;size--;// if found node is head of the list, need to change to next elementif(temp==head){head=head.next;returntrue;}// if found node is last node of the list, need to change tailif(temp==tail){tail=temp.prev;returntrue;}// change linkstemp.prev.next=temp.next;temp.next.prev=temp.prev;returntrue;}// wrapper class for list elementsprivateclassNode{Eelem;Nodenext;Nodeprev;publicNode(Eelem){this.elem=elem;}publicNode(Eelem,Nodeprev,Nodenext){this.elem=elem;this.next=next;this.prev=prev;}}}
This is the first blog about linked lists, you can read LinkedList Advanced Operations blog for
recursion, reversing and some other operation imlementations.