TGViewer
Programming Resources | Python | Javascript | Artificial Intelligence Updates | Computer Science Courses | AI Books Programming Resources | Python | Javascript | Artificial Intelligence Updates | Computer Science Courses | AI Books @programming_guide · 56K subscribers
Post #3467 2.67K
✅ DSA Part 5 – Linked Lists: Single, Double & Reverse 🔁🔗📚

Linked Lists are dynamic data structures ideal for scenarios requiring frequent insertions and deletions. Unlike arrays, they don’t need contiguous memory and offer flexible memory usage.

1️⃣ What is a Linked List?
A Linked List is a linear data structure where each element (node) contains:
- Data
- Pointer to the next node (and optionally the previous node)

Types:
- Singly Linked List: Each node points to the next
- Doubly Linked List: Nodes point to both next and previous
- Circular Linked List: Last node points back to the head

2️⃣ Singly Linked List – Basic Structure

Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


Java
class Node {
    int data;
    Node next;
    Node(int data) {
        this.data = data;
        this.next = null;
    }
}


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


3️⃣ Insert at Head (Singly)

Python
def insert_head(head, data):
    new_node = Node(data)
    new_node.next = head
    return new_node


Java
Node insertHead(Node head, int data) {
    Node newNode = new Node(data);
    newNode.next = head;
    return newNode;
}


C++
Node* insertHead(Node* head, int data) {
    Node* newNode = new Node(data);
    newNode->next = head;
    return newNode;
}


4️⃣ Doubly Linked List – Bi-directional Pointers

Python
class DNode:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None


Java
class DNode {
    int data;
    DNode prev, next;
    DNode(int data) {
        this.data = data;
    }
}


C++
struct DNode {
    int data;
    DNode* prev;
    DNode* next;
    DNode(int data): data(data), prev(nullptr), next(nullptr) {}
};


5️⃣ Insert at Head (Doubly)

Python
def insert_head(head, data):
    new_node = DNode(data)
    new_node.next = head
    if head:
        head.prev = new_node
    return new_node


Java
DNode insertHead(DNode head, int data) {
    DNode newNode = new DNode(data);
    newNode.next = head;
    if (head != null) head.prev = newNode;
    return newNode;
}


C++
DNode* insertHead(DNode* head, int data) {
    DNode* newNode = new DNode(data);
    newNode->next = head;
    if (head) head->prev = newNode;
    return newNode;
}


6️⃣ Reversing a Singly Linked List

Python
def reverse_list(head):
    prev = None
    current = head
    while current:
        next_node = current.next
        current.next = prev
        prev = current
        current = next_node
    return prev


Java
Node reverseList(Node head) {
    Node prev = null, current = head;
    while (current != null) {
        Node next = current.next;
        current.next = prev;
        prev = current;
        current = next;
    }
    return prev;
}


C++
Node* reverseList(Node* head) {
    Node* prev = nullptr;
    Node* current = head;
    while (current) {
        Node* next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    return prev;
}


7️⃣ Why Use Linked Lists?
✅ Dynamic memory allocation
✅ Efficient insert/delete (O(1) at head/tail)
❌ Slower access (O(n) for random access)
✅ Great for implementing stacks, queues, hash maps, etc.

8️⃣ Practice Tasks
✅ Implement singly linked list with insert/delete
✅ Implement doubly linked list with insert at tail
✅ Reverse a singly linked list
  • ❤ 5
More from @programming_guide
  1. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  2. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  3. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
  4. Oct 7, 2026𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗼𝘄𝗲𝗿 𝗕𝗜 𝗳𝗼𝗿 𝗙𝗥𝗘𝗘! 🔥 Learn Power BI through these FREE learnin…
  5. Sep 29, 2026🧠 Core Programming Concepts You Should Know 💻🚀 These are the fundamental ideas behind a…
  6. Sep 29, 2026𝗙𝗥𝗘𝗘 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗧𝗼 𝗟𝗲𝗮𝗿𝗻 𝗔𝗜 𝗶𝗻 𝟮𝟬𝟮𝟲🚀 ​ Explore 6 free resource…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →