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

Share
Published by
Hassan Raza

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

9 months ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

9 months ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

11 months ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

12 months ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

12 months ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

12 months ago