A linked list is a sequential data structure where data is stored in a sequential manner just like an array but what makes linked list more efficient is the way it stores data so let’s see the implementation of linked list using java. Why linked list but not an array? When not to use linked list? Operation performed on linked list What are those O[n] or O[1]? Implementation of linked list in java public class LinkedList { private Node startNode = null; private Node endNode = null; private int nodeCounter; //A node for the linked list. private class Node { private int value; private Node next; Node[int value] { this.value = value; this.next = null; } } private Node createNode[int value] { nodeCounter++; return new Node[value]; } private void insertNodeAtEnd[int value] { if [startNode == null] { startNode = createNode[value]; endNode = startNode; } else { Node node = createNode[value]; endNode.next = node; endNode = node; } } private void insertNodeAtBeginning[int value] { Node node = createNode[value];node.next = startNode; startNode = node; } private void insertAtMid[int insertAfter, int newValue] { Node node = startNode; while [node != null] { if [node.value == insertAfter] { Node newNode = createNode[newValue]; newNode.next = node.next; node.next = newNode; break; } node = node.next; } }private void deleteStartingNode[] { if [startNode != null] { nodeCounter--; startNode = startNode.next; } } private void deleteAtEnd[] { if [startNode == null] { return; } if [startNode == endNode] { nodeCounter--; startNode = endNode = null; return; } Node node = startNode; while [node.next.next != null] { node = node.next; } node.next = null; endNode = node; nodeCounter--; } private void displayNode[] { Node node = startNode; while [node != null] { System.out.println["-->"+node.value]; node = node.next; } } private void deleteParticularNode[int deleteValue] { if [startNode == null] { return; } if [startNode == endNode] { if [startNode.value == deleteValue] { startNode = endNode = null; nodeCounter--; } else { System.out.print["Value not found"]; } return; }if [startNode.value == deleteValue] { deleteStartingNode[];return; }Node node = startNode; while [node.next != null] { if [node.next.value == deleteValue] { node.next = node.next.next; nodeCounter--; break; } node = node.next; } }public static void main[String args[]] { LinkedList linkedList = new LinkedList[]; linkedList.insertNodeAtEnd[10]; linkedList.insertNodeAtEnd[20]; linkedList.insertNodeAtEnd[30]; linkedList.insertNodeAtEnd[40]; linkedList.insertNodeAtEnd[45]; linkedList.insertNodeAtEnd[50]; linkedList.insertNodeAtEnd[55]; linkedList.insertNodeAtBeginning[1]; linkedList.insertNodeAtBeginning[5]; linkedList.insertAtMid[40,45]; linkedList.displayNode[]; System.out.print["Total number of node:"+linkedList.nodeCounter]; }} Let’s simplified the code //A node for the linked list. Node[int value] { this.value = value; this.next = null; } } Node will be treated as user defined data type where the type of data that it can store is what you defined inside Node class. This node class should also have self referential object so that it can connect with other nodes. private void insertNodeAtEnd[int value] { endNode.next = node; endNode = node; } } Here startNode and endNode is instance of class Node where startNode always point to the starting of the list and endNode points to the end of the list. Initially startNode will be null so create a node and that node will be treated as startNode from the next time onward attach the node at the end than mark the newly attached node as endNode. private void insertAtMid[int insertAfter, int newValue] { newNode.next = node.next; break; } node = node.next; }} To insert the node in between first create a node and than attach the newly created node with the existing node.next than assign your node to node.next this will add the node in between. private void deleteStartingNode[] { } To delete the node from the starting of the list just move the startNode one node ahead by startNode.next that’s it. private void deleteAtEnd[] { if [startNode == null] { return; } if [startNode == endNode] { nodeCounter--; startNode = endNode = null; return; } Node node = startNode; while [node.next.next != null] { node = node.next; } node.next = null; endNode = node; nodeCounter--; } To delete the node form the end of the list first check whether the startNode is null or not if it’s null than just exit if not than check if startNode is equal to endNode if they are equal than just mark both to null and return. In case both the above conditions are not matched than check for one node before the last node and make that node.next to null as well as make it as your endNode. Linked list is widely used in most the data structures so it’s quite important to know how to perform operations on linked list that’s it for linked list keep experimenting and happy coding! Singly Linked List is a variant of Linked List which allows only forward traversal of linked lists. This is a simple form, yet it is effective for several problems such as Big Integer calculations. A singly linked list is made up of nodes where each node has two parts:
The beginning of the node marked by a special pointer named START. The pointer points to the fist node of the list but the link part of the last node has no next node to point to. The main difference from an array is:
In Singly Linked List, only the pointer to the first node is stored. The other nodes are accessed one by one. To get the address of ith node, we need to traverse all nodes before it because the address of ith node is stored with i-1th node and so on. Given a singly linked list and a key, find key using binary search approach.To perform a Binary search based on Divide and Conquer Algorithm, determination of the middle element is important. Binary Search is usually fast and efficient for arrays because accessing the middle index between two given indices is easy and fast[Time Complexity O[1]]. But memory allocation for the singly linked list is dynamic and non-contiguous, which makes finding the middle element difficult. One approach could be of using skip list, one could be traversing the linked list using one pointer. Prerequisite: Finding middle of a linked list. Note: The approach and implementation provided below are to show how Binary Search can be implemented on a linked list. The implementation takes O[n] time. Approach :
In main function, function InsertAtHead inserts value at the beginning of linked list. Inserting such values[for sake of simplicity] so that the list created is sorted.Examples : Input : Enter value to search : 7 Output : Found Input : Enter value to search : 12 Output : Not FoundC++
Java
Python
C#
Javascript
Output: Present Time Complexity: O[N] Article Tags : Divide and Conquer Linked List Searching Binary Search Practice Tags : Linked List Searching Divide and Conquer Binary Search Read Full Article Hi there! This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science. When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sorting algorithms so that I wouldn't be stumped when asked about them. Over the last few years, I've interviewed at several Silicon Valley startups, and also some bigger companies, like Google, Facebook, Yahoo, LinkedIn, and Uber, and each time that I prepared for an interview, I thought to myself "Why hasn't someone created a nice Big-O cheat sheet?". So, to save all of you fine folks a ton of time, I went ahead and created one. Enjoy! - Eric Check out El Grapho, a graph data visualization library that supports millions of nodes and edges |