A linked list is a linear data structure composed of nodes, each of which holds a value and a reference (or pointer) to the next node in the list. The first node is referred to as the list’s head, while the last node is referred to as the list’s tail.
Because linked lists are dynamic data structures, their size can be easily changed during programme execution by adding or removing nodes. As a result, they are effective in circumstances where the size of the data being saved is unknown or can change dynamically.
A linked list node has two fields:
Linked lists and arrays are two common data structures used in programming, and each has benefits and drawbacks. Here are some of the advantages of using a linked list versus an array:
The linked list mainly has three types, they are:
Linked lists support several operations, some of which are:
Node current = head;
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
// Definition of a singly linked list node
class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}
// Inserting a node at the beginning of a singly linked list
public ListNode insertAtBeginning(ListNode head, int val) {
ListNode newNode = new ListNode(val);
newNode.next = head;
return newNode;
}
-
-
-
-
-
-
// Definition of a doubly linked list node
class ListNode {
int val;
ListNode prev;
ListNode next;
ListNode(int val) { this.val = val; }
}
// Inserting a node at the end of a doubly linked list
public ListNode insertAtEnd(ListNode head, int val) {
ListNode newNode = new ListNode(val);
if (head == null) {
return newNode;
}
ListNode curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = newNode;
newNode.prev = curr;
return head;
}
// Deleting a node with a specific value from a singly linked list
public ListNode deleteNode(ListNode head, int val) {
if (head == null) {
return null;
}
if (head.val == val) {
return head.next;
}
ListNode prev = head;
ListNode curr = head.next;
while (curr != null) {
if (curr.val == val) {
prev.next = curr.next;
return head;
}
prev = curr;
curr = curr.next;
}
return head;
}
public class LinkedList {
Node head; // head of the list
// Node class
static class Node {
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
// Search an element in the linked list
public boolean search(Node head, int x)
{
Node current = head; // Initialize current
while (current != null) {
if (current.data == x)
return true; // data found
current = current.next;
}
return false; // data not found
}
// Driver Code
public static void main(String args[])
{
LinkedList list = new LinkedList();
list.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
list.head.next = second; // Link first node with the second node
second.next = third; // Link second node with the third node
int searchData = 3;
if (list.search(list.head, searchData))
System.out.println(searchData + " is present in the list");
else
System.out.println(searchData + " is not present in the list");
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(5);
list.add(1);
list.add(4);
list.add(2);
list.add(3);
// Using Collections.sort() method
Collections.sort(list);
System.out.println("Sorted list: " + list);
}
}
// Reversing a singly linked list
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
Here’s a table summarizing the time and space complexity for various operations in a linked list:
Operation | Time Complexity | Space Complexity |
---|---|---|
Insertion at Head | O(1) | O(1) |
Insertion at Tail | O(n) | O(1) |
Insertion at Index | O(n) | O(1) |
Deletion at Head | O(1) | O(1) |
Deletion at Tail | O(n) | O(1) |
Deletion at Index | O(n) | O(1) |
Search | O(n) | O(1) |
Access by Index | O(n) | O(1) |
Length/Size | O(n) | O(1) |
Reverse | O(n) | O(1) |
Concatenation | O(1) | O(1) |
Split | O(n) | O(1) |
Note: also read about Recursion in DSA
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…