அல்கோரிதம்linked list

Linked List — Node & next

TT
Testlaa Team
May 14, 20261 min read

Singly linked listNode சங்கிலி; ஒவ்வொன்றும் next reference.

Try this in Python

from __future__ import annotations
from dataclasses import dataclass


@dataclass
class Node:
    val: int
    next: Node | None = None


def to_list(head: Node | None) -> list[int]:
    out: list[int] = []
    while head:
        out.append(head.val)
        head = head.next
    return out


def from_list(vals: list[int]) -> Node | None:
    dummy = Node(0)
    cur = dummy
    for x in vals:
        cur.next = Node(x)
        cur = cur.next
    return dummy.next

head = from_list([1, 2, 3])
print(to_list(head))

Key takeaways

  • Diagram முன்.
  • Empty — head is None.
  • Insert head — dummy pattern.

Tags:

Linked listsPythonமாணவர்கள்