Singly Linked List in Java

Deep Dive into Singly linked list in Java

Singly Linked List in Java is one of the most powerful data structures there. Let us have a look on how we can write the Java Program for a singly linked List below

java

What is a Singly Linked List in Java?

A singly Linked List in Java is a collection of nodes that are connected in a chained sequence. Where each node has the following –

  • Data Value
  • Next Reference – Reference to the next node in the sequence

Unlike Arrays with are contiguously stored, Linked List is not contiguous and are scattered all over the memory but connected with one another using the next references.

Also, array size can not be dynamically increased however, in Linked List the size can be increased and decreased at any time dynamically.

Singly linked list in Java

Structure of singly linked list in Java

class LinkedList {
  Node head; // head
  
  // Linked list Node
  class Node {
    int data;
    Node next;

    // constructor to initialize
    Node(int d) { 
      data = d;
next = null; } } }

Operation on singly linked list

  • Deletion 
  • Insertion
  • Traversals

Insertion of a node

Singly linked list in C 3

Deletion of a node

Singly linked list in C 5

Traversal in a Singly Linked List

Singly linked list in C 1

Code for Implementing Single Linked List in Java

WHY?
SINGLY LINKED LIST !!!

  • Dynamic Data Structure 

        Linked list is a dynamic in nature so it can grow and shrink at runtime by allocating and deallocating memory. 

  • Insertion and Deletion 

          Insertion and Deletion becomes easy in linked list .

  • Memory Wastage 

        As linked list is dynamic in nature we can increase as  well as decrease the size of list thus memory is saved 

  • Implement 

         Linked list are widely used in programming of stacks and queues .

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Time Complexity
For Singly Linked List

Best

O(1)

Average

O(n)

Worst

O(n)

Average Comparisons

(n+1)/2

Time Complexity
For Singly Linked List

Best

O(1)

Average

O(n)

Worst

O(n)

Average Comparisons

(n+1)/2

Quiz time

Quiz Time

Question 1. What is the time complexity for searching an element in a singly linked list, assuming the element is present in the list?

  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n^2)

The correct answer is c) O(n).

Searching for an element in a singly linked list requires traversing through each node until the desired element is found or the end of the list is reached.
In the worst case, when the element is at the end of the list or not present at all, the algorithm needs to visit every node, resulting in a linear time complexity, O(n).

Question 2. Which of the following operations has a time complexity of O(1) in a singly linked list?

  1. Insertion at the beginning
  2. Deletion at the end
  3. Traversal
  4. Searching for an element

The correct answer is a)

Insertion at the beginning. In a singly linked list, insertion at the beginning involves updating only a few pointers, irrespective of the size of the list.
Therefore, the time complexity of this operation is constant, O(1). The other options require traversing the list, which takes linear time, O(n).

Question 3. What is the time complexity to search for an element in a singly linked list of length ‘n’ in the worst case?

  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n^2)

Answer: c) Insertion in sorted order

Explanation: Insertion in sorted order can be performed efficiently on a sorted singly linked list. By traversing the list and finding the appropriate position for the new node, we can insert it in the correct place while maintaining the sorted order. This operation requires traversing the list in a manner similar to searching, resulting in a time complexity of O(n) in the worst case.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription