How to Implement Singly Linked List in Python

PHPz
Release: 2024-09-10 06:46:02
Original
1016 people have browsed it

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()

Copy after login

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.

The above is the detailed content of How to Implement Singly Linked List in Python. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!