WikiGalaxy

Personalize

Traversal on Singly Linked List

Concept Overview:

A singly linked list is a data structure that consists of a sequence of elements, where each element points to the next one. Traversal involves visiting each element to perform operations like insertion, deletion, or simply accessing the data.

Operations:

  • Insert at Front: Add a new node at the beginning of the list.
  • Insert at End: Add a new node at the end of the list.
  • Insert at Middle: Add a new node at a specified position.
  • Delete from Front: Remove the first node of the list.
  • Delete from End: Remove the last node of the list.
  • Delete from Middle: Remove a node from a specified position.
    Step 1: Initialize the list
    [Head] -> [Data | Next] -> [Data | Next] -> [Data | Null]

    Step 2: Insert at Front
    [New Head] -> [Old Head] -> [Data | Next] -> [Data | Null]

    Step 3: Insert at End
    [Head] -> [Data | Next] -> [Data | Next] -> [Data | New Null]

    Step 4: Insert at Middle
    [Head] -> [Data | Next] -> [New Data | Next] -> [Data | Null]

    Step 5: Delete from Front
    [New Head] -> [Data | Next] -> [Data | Null]

    Step 6: Delete from End
    [Head] -> [Data | Next] -> [Data | New Null]

    Step 7: Delete from Middle
    [Head] -> [Data | Next] -> [Data | New Next]
  

class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class LinkedList {
    Node head;

    public void insertAtFront(int newData) {
        Node newNode = new Node(newData);
        newNode.next = head;
        head = newNode;
    }

    public void insertAtEnd(int newData) {
        Node newNode = new Node(newData);
        if (head == null) {
            head = new Node(newData);
            return;
        }
        newNode.next = null;
        Node last = head;
        while (last.next != null)
            last = last.next;
        last.next = newNode;
    }

    public void insertAtPosition(int position, int newData) {
        Node newNode = new Node(newData);
        if (position == 0) {
            newNode.next = head;
            head = newNode;
            return;
        }
        Node prev = head;
        for (int i = 0; i < position - 1 && prev != null; i++)
            prev = prev.next;
        if (prev == null || prev.next == null) return;
        newNode.next = prev.next;
        prev.next = newNode;
    }

    public void deleteFromFront() {
        if (head != null) head = head.next;
    }

    public void deleteFromEnd() {
        if (head == null || head.next == null) {
            head = null;
            return;
        }
        Node secondLast = head;
        while (secondLast.next.next != null)
            secondLast = secondLast.next;
        secondLast.next = null;
    }

    public void deleteAtPosition(int position) {
        if (head == null) return;
        Node temp = head;
        if (position == 0) {
            head = temp.next;
            return;
        }
        for (int i = 0; temp != null && i < position - 1; i++)
            temp = temp.next;
        if (temp == null || temp.next == null) return;
        Node next = temp.next.next;
        temp.next = next;
    }
}
    

How the Code Works:

The code defines a simple singly linked list with basic operations for insertion and deletion. Each operation is defined as a method within the LinkedList class, allowing for modular and easy-to-read code.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025