Categories: Data Structurepython

Implementation of Singly Linked List in Python

In a singly linked list, we have an ordered list of items as individual Nodes that have pointers to other Nodes.

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

Now we can build out Linked List with the collection of nodes:

a = Node(1)
b = Node(2)
c = Node(3)
a.nextnode = b
b.nextnode = c

In a Linked List the first node is called the head and the last node is called the tail. Let’s discuss the pros and cons of Linked Lists:

Pros 😍😘

  • Linked Lists have constant-time insertions and deletions in any position, in comparison, arrays require O(n) time to do the same thing.
  • Linked lists can continue to expand without having to specify their size ahead of time.

Cons 😒😣

  • To access an element in a linked list, you need to take O(k) time to go from the head of the list to the kth element. In contrast, arrays have constant time operations to access elements in an array.

Recommended: Understand The Singly Linked List and its Operation

Follow Me ❀😊

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

View Comments

Recent Posts

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate Ο€ Using Monte Carlo Method

The formula for the area of a circle is given by Ο€rΒ². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago