Both Singly linked list and Doubly linked list are the implementation of Linked list in which every element of singly-linked list contains some data and a link to the next element, which allows to keep the structure. On the other hand, every node in a doubly-linked list also contains a link to the previous node.
The following are the important differences between a Singly linked list and Doubly linked list.
Sr. No. | Key | Singly linked list | Doubly linked list |
---|---|---|---|
1 | Complexity | In singly linked list the complexity of insertion and deletion at a known position is O(n) | In case od doubly linked list the complexity of insertion and deletion at a known position is O(1) |
2 | Internal implementation | In singly linked list implementation is such as where the node contains some data and a pointer to the next node in the list | While doubly linked list has some more complex implementation where the node contains some data and a pointer to the next as well as the previous node in the list |
3 | Order of elements | Singly linked list allows traversal elements only in one way. | Doubly linked list allows element two way traversal. |
4 | Usage | Singly linked list are generally used for implementation of stacks | On other hand doubly linked list can be used to implement stacks as well as heaps and binary trees. |
5 | Index performance | Singly linked list is preferred when we need to save memory and searching is not required as pointer of single index is stored. | If we need better performance while searching and memory is not a limitation in this case doubly linked list is more preferred. |
6 | Memory consumption | As singly linked list store pointer of only one node so consumes lesser memory. | On other hand Doubly linked list uses more memory per node(two pointers). |
SinlgyLinkedList.java
class Node { //create class Node public int data; public Node next; //create node parameter for pointer of next element public void displayNodeData() { System.out.println("{ " + data + " } "); } } public class SinglyLinkedList { private Node head; public boolean isEmpty() { return (head == null); } // used to insert a node at the start of linked list public void insertFirst(int data) { Node newNode = new Node(); newNode.data = data; newNode.next = head; head = newNode; } // used to delete node from start of linked list public Node deleteFirst() { Node temp = head; head = head.next; return temp; } // Use to delete node after particular node public void deleteAfter(Node after) { Node temp = head; while (temp.next != null && temp.data != after.data) { temp = temp.next; } if (temp.next != null) temp.next = temp.next.next; } // used to insert a node at the start of linked list public void insertLast(int data) { Node current = head; while (current.next != null) { current = current.next; // we'll loop until current.next is null } Node newNode = new Node(); newNode.data = data; current.next = newNode; } // For printing Linked List public void printLinkedList() { System.out.println("Printing LinkedList (head --> last) "); Node current = head; while (current != null) { current.displayNodeData(); current = current.next; } System.out.println(); } }
LinkedListMain.java
public class LinkedListMain { public static void main(String args[]){ SinglyLinkedList myLinkedlist = new SinglyLinkedList(); myLinkedlist.insertFirst(5); myLinkedlist.insertFirst(6); myLinkedlist.insertFirst(7); myLinkedlist.insertFirst(1); myLinkedlist.insertLast(2); Node node=new Node(); node.data=1; myLinkedlist.deleteAfter(node); myLinkedlist.printLinkedList(); } }
Printing LinkedList (head --> last) { 1 } { 6 } { 5 } { 2 }
DoublyLinkedListMain.java
public class DoublyLinkedList { class Node{ int data; Node previous; Node next; public Node(int data) { this.data = data; } } Node head, tail = null; public void addNode(int data) { //Create a new node Node newNode = new Node(data); if(head == null) { head = tail = newNode; head.previous = null; tail.next = null; } else { tail.next = newNode; newNode.previous = tail; tail = newNode; tail.next = null; } } public void display() { Node current = head; if(head == null) { System.out.println("List is empty"); return; } System.out.println("Nodes of doubly linked list: "); while(current != null) { System.out.print(current.data + " "); current = current.next; } } public static void main(String[] args) { DoublyLinkedList dList = new DoublyLinkedList(); dList.addNode(1); dList.addNode(2); dList.addNode(3); dList.addNode(4); dList.addNode(5); dList.display(); } }
Nodes of doubly linked list: 1 2 3 4 5