DEV Community

thirumaleshthiru
thirumaleshthiru

Posted on

1

How to Implement Singly Linked List in Python

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

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

    def add_front(self,value):
        new_node = Node(value)
        new_node.next = self.head
        self.head = new_node
    def add_back(self,value):
        new_node = Node(value)
        if self.head is None:
            self.head = new_node
        else:
            current = self.head
            while current.next is not None:
                current = current.next
            current.next = new_node
    def print_list(self):
        current = self.head
        while current is not None:
            print(current.value)
            current = current.next

list1 = LinkedList()

list1.add_front(1)
list1.add_front(2)
list1.add_back(3)
list1.print_list()

Enter fullscreen mode Exit fullscreen mode

1. Node Class:

  • Represents an individual element in the linked list.
  • Each node has two attributes: value to store data and next to point to the next node in the list.
  • When a node is created, its next pointer is set to None.

2. LinkedList Class:

  • Manages the linked list operations.
  • Has an attribute head which is the starting point of the linked list. Initially, head is set to None since the list is empty.

3. add_front Method:

  • Adds a new node to the front of the linked list.
  • A new Node is created with the given value.
  • The new node's next pointer is set to the current head of the list.
  • The head of the list is then updated to the new node.

4. add_back Method:

  • Adds a new node to the end of the linked list.
  • A new Node is created with the given value.
  • If the list is empty (i.e., head is None), the new node is set as the head.
  • If the list is not empty, it traverses to the end of the list, then updates the last node's next pointer to point to the new node.

5. print_list Method:

  • Prints all the values in the linked list from the head to the end.
  • Starts from the head and iterates through each node using the next pointer until it reaches the end (None), printing the value of each node.

6. Usage Example:

  • An instance of LinkedList is created.
  • add_front is called twice to add nodes with values 1 and 2 to the front of the list.
  • add_back is called to add a node with value 3 to the end of the list.
  • print_list is called to print the values of all nodes in the linked list. The output is 2, 1, 3, showing that nodes were added correctly.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay