Tutorials Logic, IN info@tutorialslogic.com

Linked List Singly, Insert, Delete, Reverse: Tutorial, Examples, FAQs & Interview Tips

Linked List Singly, Insert, Delete, Reverse

Linked List Singly, Insert, Delete, Reverse is an important Data Structure topic because it shows up in real projects, debugging sessions, and interviews. Learn the meaning first, then connect it to a small working example so the rule does not stay abstract.

Focus on what problem Linked List Singly, Insert, Delete, Reverse solves, where developers usually make mistakes, and how to verify the result with output, behavior, or a small test.

A strong understanding of Linked List Singly, Insert, Delete, Reverse should include syntax, behavior, one realistic use case, one failure case, and one quick way to check your work.

Linked List Singly Insert Delete Reverse should be studied as a practical Data Structure lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.

In the data-structure > linked-list page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.

What is a Linked List?

A linked list is a linear data structure where elements (called nodes) are stored in non-contiguous memory locations. Each node contains data and a pointer to the next node. Unlike arrays, linked lists don't require contiguous memory and can grow/shrink dynamically.

Think of a linked list like a treasure hunt - each clue (node) tells you where the next clue is. You can't jump directly to clue #5; you must follow the chain from the beginning.

Array vs Linked List

Feature Array Linked List
Memory Contiguous Non-contiguous (scattered)
Access by index O(1) - direct O(n) - must traverse
Insert at beginning O(n) - shift elements O(1)
Insert at end O(1) if space O(n) or O(1) with tail pointer
Delete O(n) - shift elements O(1) with pointer to node
Memory overhead None Extra pointer per node
Cache performance Excellent Poor (scattered memory)

Singly Linked List - Full Implementation

Singly Linked List - Insert, Delete, Search, Reverse

Singly Linked List - Insert, Delete, Search, Reverse
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
    Node(int d) : data(d), next(nullptr) {}
};

class LinkedList {
    Node* head;
public:
    LinkedList() : head(nullptr) {}

    void insertFront(int data) {
        Node* node = new Node(data);
        node->next = head;
        head = node;
    }

    void insertEnd(int data) {
        Node* node = new Node(data);
        if (!head) { head = node; return; }
        Node* curr = head;
        while (curr->next) curr = curr->next;
        curr->next = node;
    }

    void deleteVal(int data) {
        if (!head) return;
        if (head->data == data) { Node* t = head; head = head->next; delete t; return; }
        Node* curr = head;
        while (curr->next && curr->next->data != data) curr = curr->next;
        if (curr->next) { Node* t = curr->next; curr->next = t->next; delete t; }
    }

    bool search(int data) {
        Node* curr = head;
        while (curr) { if (curr->data == data) return true; curr = curr->next; }
        return false;
    }

    void reverse() {
        Node *prev = nullptr, *curr = head, *next = nullptr;
        while (curr) { next = curr->next; curr->next = prev; prev = curr; curr = next; }
        head = prev;
    }

    void print() {
        Node* curr = head;
        while (curr) { cout << curr->data << " -> "; curr = curr->next; }
        cout << "NULL" << endl;
    }
};

int main() {
    LinkedList ll;
    ll.insertEnd(1); ll.insertEnd(2); ll.insertEnd(3);
    ll.insertFront(0);
    ll.print();       // 0 -> 1 -> 2 -> 3 -> NULL
    ll.deleteVal(2);
    ll.print();       // 0 -> 1 -> 3 -> NULL
    ll.reverse();
    ll.print();       // 3 -> 1 -> 0 -> NULL
    cout << boolalpha << ll.search(1) << endl;  // true
    return 0;
}

Singly Linked List - Full Implementation

Singly Linked List - Full Implementation
public class LinkedList {
    static class Node {
        int data;
        Node next;
        Node(int d) { data = d; next = null; }
    }

    Node head;

    void insertFront(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    void insertEnd(int data) {
        Node node = new Node(data);
        if (head == null) { head = node; return; }
        Node curr = head;
        while (curr.next != null) curr = curr.next;
        curr.next = node;
    }

    void delete(int data) {
        if (head == null) return;
        if (head.data == data) { head = head.next; return; }
        Node curr = head;
        while (curr.next != null && curr.next.data != data) curr = curr.next;
        if (curr.next != null) curr.next = curr.next.next;
    }

    void reverse() {
        Node prev = null, curr = head, next = null;
        while (curr != null) { next = curr.next; curr.next = prev; prev = curr; curr = next; }
        head = prev;
    }

    void print() {
        Node curr = head;
        while (curr != null) { System.out.print(curr.data + " -> "); curr = curr.next; }
        System.out.println("NULL");
    }

    public static void main(String[] args) {
        LinkedList ll = new LinkedList();
        ll.insertEnd(1); ll.insertEnd(2); ll.insertEnd(3);
        ll.insertFront(0);
        ll.print();   // 0 -> 1 -> 2 -> 3 -> NULL
        ll.delete(2);
        ll.print();   // 0 -> 1 -> 3 -> NULL
        ll.reverse();
        ll.print();   // 3 -> 1 -> 0 -> NULL
    }
}

Singly Linked List - Full Implementation

Singly Linked List - Full Implementation
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def insert_front(self, data):
        node = Node(data)
        node.next = self.head
        self.head = node

    def insert_end(self, data):
        node = Node(data)
        if not self.head:
            self.head = node
            return
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = node

    def delete(self, data):
        if not self.head: return
        if self.head.data == data:
            self.head = self.head.next
            return
        curr = self.head
        while curr.next and curr.next.data != data:
            curr = curr.next
        if curr.next:
            curr.next = curr.next.next

    def reverse(self):
        prev, curr = None, self.head
        while curr:
            curr.next, prev, curr = prev, curr, curr.next
        self.head = prev

    def print_list(self):
        curr = self.head
        result = []
        while curr:
            result.append(str(curr.data))
            curr = curr.next
        print(" -> ".join(result) + " -> NULL")

ll = LinkedList()
ll.insert_end(1); ll.insert_end(2); ll.insert_end(3)
ll.insert_front(0)
ll.print_list()   # 0 -> 1 -> 2 -> 3 -> NULL
ll.delete(2)
ll.print_list()   # 0 -> 1 -> 3 -> NULL
ll.reverse()
ll.print_list()   # 3 -> 1 -> 0 -> NULL

Singly Linked List - Full Implementation

Singly Linked List - Full Implementation
class Node {
    constructor(data) { this.data = data; this.next = null; }
}

class LinkedList {
    constructor() { this.head = null; }

    insertFront(data) {
        const node = new Node(data);
        node.next = this.head;
        this.head = node;
    }

    insertEnd(data) {
        const node = new Node(data);
        if (!this.head) { this.head = node; return; }
        let curr = this.head;
        while (curr.next) curr = curr.next;
        curr.next = node;
    }

    delete(data) {
        if (!this.head) return;
        if (this.head.data === data) { this.head = this.head.next; return; }
        let curr = this.head;
        while (curr.next && curr.next.data !== data) curr = curr.next;
        if (curr.next) curr.next = curr.next.next;
    }

    reverse() {
        let prev = null, curr = this.head;
        while (curr) {
            const next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        this.head = prev;
    }

    print() {
        const parts = [];
        let curr = this.head;
        while (curr) { parts.push(curr.data); curr = curr.next; }
        console.log(parts.join(" -> ") + " -> NULL");
    }
}

const ll = new LinkedList();
ll.insertEnd(1); ll.insertEnd(2); ll.insertEnd(3);
ll.insertFront(0);
ll.print();   // 0 -> 1 -> 2 -> 3 -> NULL
ll.delete(2);
ll.print();   // 0 -> 1 -> 3 -> NULL
ll.reverse();
ll.print();   // 3 -> 1 -> 0 -> NULL

Linked List Singly Insert Delete Reverse normal path trace

Linked List Singly Insert Delete Reverse normal path trace
1. Define the input for Linked List Singly Insert Delete Reverse.
2. Apply the rule from the lesson.
3. Compare the actual result with the expected result.
4. Record the fix if the result differs.

Linked List Singly Insert Delete Reverse edge path trace

Linked List Singly Insert Delete Reverse edge path trace
1. Try empty, missing, duplicate, or invalid data.
2. Identify where Linked List Singly Insert Delete Reverse changes behavior.
3. Explain the safest correction.
4. Retest the normal path.
Key Takeaways
  • Explain the purpose of Linked List Singly, Insert, Delete, Reverse before memorizing syntax.
  • Run or trace one small Data Structure example and confirm the output.
  • Test one normal case, one edge case, and one mistake case for Linked List Singly, Insert, Delete, Reverse.
  • Write the rule in your own words after checking the example.
  • Connect Linked List Singly, Insert, Delete, Reverse to a real project scenario instead of treating it as an isolated definition.
Common Mistakes to Avoid
WRONG Memorizing Linked List Singly Insert Delete Reverse without the situation where it is useful.
RIGHT Connect Linked List Singly Insert Delete Reverse to a concrete Data Structure task.
Purpose makes syntax easier to recall.
WRONG Testing Linked List Singly Insert Delete Reverse only with the perfect input.
RIGHT Include empty, missing, duplicate, incompatible, or failed cases when relevant.
Real bugs usually appear outside the perfect path.
WRONG Changing code before reading the visible symptom or error message.
RIGHT Inspect the output, state, configuration, or stack trace connected to Linked List Singly Insert Delete Reverse.
Evidence keeps debugging focused.
WRONG Memorizing Linked List Singly Insert Delete Reverse without the situation where it is useful.
RIGHT Connect Linked List Singly Insert Delete Reverse to a concrete Data Structure task.
Purpose makes syntax easier to recall.

Practice Tasks

  • Modify the example so it handles a different input or condition.
  • Write one mistake related to Linked List Singly, Insert, Delete, Reverse, then fix it and explain the fix.
  • Summarize when to use Linked List Singly, Insert, Delete, Reverse and when another approach is better.
  • Write a small example that uses Linked List Singly Insert Delete Reverse in a realistic Data Structure scenario.
  • Change one important value in the Linked List Singly Insert Delete Reverse example and predict the result first.

Frequently Asked Questions

Arrays: O(1) random access, O(n) insert/delete in middle, fixed or dynamic size, contiguous memory. Linked lists: O(n) access, O(1) insert/delete at known position, dynamic size, non-contiguous memory.

Use Floyd's cycle detection (fast and slow pointers). Move slow by 1 step and fast by 2 steps. If they meet, there is a cycle. Time: O(n), Space: O(1).

Use two pointers: slow moves 1 step, fast moves 2 steps. When fast reaches the end, slow is at the middle. Time: O(n), Space: O(1).

Iterative: use three pointers (prev=null, curr=head, next). Loop: save next, point curr.next to prev, move prev to curr, move curr to next. Return prev. Time: O(n), Space: O(1).

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.