<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: thirumaleshthiru</title>
    <description>The latest articles on Forem by thirumaleshthiru (@thirumalesh).</description>
    <link>https://forem.com/thirumalesh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1430414%2F675191d4-3270-4d31-9f74-fc403d72e79c.png</url>
      <title>Forem: thirumaleshthiru</title>
      <link>https://forem.com/thirumalesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thirumalesh"/>
    <language>en</language>
    <item>
      <title>How to Implement Singly Linked List in Python</title>
      <dc:creator>thirumaleshthiru</dc:creator>
      <pubDate>Mon, 09 Sep 2024 14:24:49 +0000</pubDate>
      <link>https://forem.com/thirumalesh/how-to-implement-singly-linked-list-in-python-40kc</link>
      <guid>https://forem.com/thirumalesh/how-to-implement-singly-linked-list-in-python-40kc</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;1. Node Class:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Represents an individual element in the linked list.&lt;/li&gt;
&lt;li&gt;Each node has two attributes: value to store data and next to point to the next node in the list.&lt;/li&gt;
&lt;li&gt;When a node is created, its next pointer is set to None.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. LinkedList Class:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manages the linked list operations.&lt;/li&gt;
&lt;li&gt;Has an attribute head which is the starting point of the linked list. Initially, head is set to None since the list is empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. add_front Method:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adds a new node to the front of the linked list.&lt;/li&gt;
&lt;li&gt;A new Node is created with the given value.&lt;/li&gt;
&lt;li&gt;The new node's next pointer is set to the current head of the list.&lt;/li&gt;
&lt;li&gt;The head of the list is then updated to the new node.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. add_back Method:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adds a new node to the end of the linked list.&lt;/li&gt;
&lt;li&gt;A new Node is created with the given value.&lt;/li&gt;
&lt;li&gt;If the list is empty (i.e., head is None), the new node is set as the head.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. print_list Method:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prints all the values in the linked list from the head to the end.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. Usage Example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An instance of LinkedList is created.&lt;/li&gt;
&lt;li&gt;add_front is called twice to add nodes with values 1 and 2 to the front of the list.&lt;/li&gt;
&lt;li&gt;add_back is called to add a node with value 3 to the end of the list.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>datastructures</category>
      <category>python</category>
      <category>linkedlist</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
