Singly Linked List
Let's do the implementation and understand the singly linked list in a deep-dive.
NOTE: -Generic <T> type can accept any data type, If you have not passed explicitly then it will consider it as an Object.
NOTE: -Generic <T> type can accept any data type, If you have not passed explicitly then it will consider it as an Object.
#1 Create a Custom Node
#2 Create Custom Singly Linked List-O(1)
#3 Adding Node at the end-O(1)
#4 Adding Node at the beginning-O(1)
#5 Adding node at desire Index-O(n)
#6 Printing node's element-O(n)
class Node<T> { T data; Node next; public Node(T data) { this.data = data; } }It is our generic custom linked list implementation, which holds the value of any type and the address of the next node.
#2 Create Custom Singly Linked List-O(1)
public class CustomLinkedList<T> { Node<T> head, tail; int size = 0; }Here, we will store the start and last address reference along with the number of elements in the Linked List.
#3 Adding Node at the end-O(1)
/** * Inserting element at the last position * Time/Space Complexity - O(1) * @param value */ public void add(T value) { Node<T> node = new Node<>(value); size++; if (head == null) { head = node; tail = head; } else { tail.next = node; tail = tail.next; } }In the code above, we are initially checking if the linked list exists. If the list is not present then create a new node and set the reference of it to head and tail otherwise tail's next will hold the address of the new node and the tail will be updated to the new node itself.
#4 Adding Node at the beginning-O(1)
/** * Insertion element at the first position * Time/Space Complexity - O(1) * @param value */ public void addFirst(T value) { Node<T> node = new Node<>(value); size++; if (head == null) { head = node; tail = head; } else { node.next = head; head = node; } }We have to check the base condition whether the list exists or not. We are creating a new node and set node's next to the head and update the head to the node itself.
#5 Adding node at desire Index-O(n)
/** * Insertion node at the particular index. * Time Complexity - O(n) * Space Complexity - O(1) * @param index * @param value */ public void addElementAtDesireIndex(int index, T value) { if (index > 0 && index <= size) { Node<T> node = new Node<>(value); if (head == null) { head = node; tail = head; } else if (index == 1) { node.next = head; head = node; } else { int count = 1; Node ref = head; while (count < index - 1) { ref = ref.next; count++; } Node tempRef = ref.next; ref.next = node; ref.next.next = tempRef; } size++; } else throw new ArrayIndexOutOfBoundsException(index); }Here, we are starting the index from 1, and checking the base condition of the linked list exists and the node needs to be added as a starting node otherwise we will traverse the node till index -1 and add a new node in between of index and index-1 node.
#6 Printing node's element-O(n)
/** * Traversing a linked list * Time Complexity - O(n) * Space Complexity - O(1) */ public void printMe() { Node ref = head; while (ref != null) { System.out.print(ref.data+" "); ref = ref.next; } }#7 Searching element-o(n)
/** * Search Value is exist in Singly linked list * Time Complexity - O(n) * Space Complexity - O(1) * @param value * @return */ public boolean contains(T value) { Node ref = head; while (ref != null) { if (ref.data == value) return true; ref = ref.next; } return false; }#8 Remove Node from beginning-O(1)
public void remove() { if (size == 1) { head = tail = null; } else { head = head.next; } size--; }#9 Remove node from last-O(n)
public void removeLast() { if (size == 1) { head = tail = null; } else { Node ref = head; while (ref.next != tail) { ref = ref.next; } ref.next = null; tail = ref; } size--; }#10 Remove node from desire location-O(n)
public void removeElementAtDesireIndex(int index) { if (index > 0 && index <= size) { if (size == 1) { head = tail = null; } else if (index == 1) { head = head.next; } else { Node ref = head; int count = 1; while (count < index - 1) { ref = ref.next; count++; } if (ref.next != null){ ref.next = ref.next.next; } else tail = ref; } size--; } else throw new ArrayIndexOutOfBoundsException(index); }#11 Clear All Linked List-O(1)
public void clear() { head = tail = null; size--; }
Comments
Post a Comment