Python Linked List: Create a doubly linked list, append some items and iterate through the listLast update on January 04 2021 14:03:01 (UTC/GMT +8 hours) Show
Doubly Linked List (Python Code with Example)Ctrl+Z, can you even survive a day without these keys? Well, Apparently not, undo and redo features are one of the used functionalities in computer engineering. But have you ever thought about how is it even possible? I happen to have an answer this time! Undo and Redo functionality is one of the most important applications of a doubly linked list. So what exactly is a doubly linked list? Before that let’s have a quick glance at Linked List: Defining a LinkedListYou already have a basic idea of what a LinkedList is, and how it works, but I’ll reiterate for clarity: According to this GeeksForGeeks article…
With this in mind, let’s talk about how to actually iterate through one. Linked Lists vs Arrays
Types of Linked ListsThere are 4 types of linked lists that can be created in python.
Singly Linked ListIn a singly linked list, each node holds a value and a reference/link to the next node. It has no link pointing to the previous node. This is the simplest form of a linked list. Circular Singly Linked ListThe circular singly linked list is similar to a singly linked list. The only difference is that the last node holds a reference to the first node of the linked list. When we reach the last node of the circular singly linked list, we have an option of going back to the first node. This is not possible in the case of a singly linked list. Doubly Linked ListIn a doubly-linked list, each node of the list has two references, one of the previous node and the other of the next node. Thus, each node holds two addresses along with the data value. Since each node has two references, a doubly-linked list allows us to traverse in both directions. Circular Doubly Linked ListCircular doubly linked lists are similar to doubly linked lists. The only difference is that their first and last nodes are interconnected as well. The head node holds a reference to the tail node and vice versa. Hence, in a circular doubly linked list we can perform circular traversal by jumping from the first node to the last node or from the last node to the first node. Creation of Singly Linked ListWe can create a singly linked list in python by following the mentioned steps.
The iterator functionSince the custom-created linked list is not iterable, we have to add an “__iter__” function so as to traverse through the list. #Iterator Function def __iter__(self): node = self.head while node: yield node node = node.nextThe time complexity for initializing a singly linked list is O(1). The space complexity is O(1) as no additional memory is required to initialize the linked list. Insertion in Singly Linked ListA node can be inserted in a singly linked list in any one of the following three ways.
Insertion AlgorithmAlgorithm to insert a node at the beginning
Method to insert a node at the beginning#Insertion at the beginning in a Singly Linked List class Node: def __init__(self, value = None): self.value = value self.next = None class SinglyLinkedList: def __init__(self): self.head = None self.tail = None #Function to add node in the beginning def atBeg(self, value): new_node = Node(value) new_node.next = self.head self.head = new_node #Initially, we have a linked list (1,3,5,7,9) called "sll". sll.atBeg(0) print([node.value for node in sll]) #Output [0, 1, 3, 5, 7, 9]Algorithm to insert a node in between the linked list
Method to insert a node in between the linked list#Insertion in between a Singly Linked List #Function to add node in the middle def inMid(self,mid_node, value): if mid_node is None: print("Mentioned node doesn't exist") return new_node = Node(value) new_node.next = mid_node.next mid_node.next = new_node #Initially, we have a linked list (1,3,5,7,9) called "sll" and "mid" node points to the value 3. sll.inMid(mid, 11) print([node.value for node in sll]) #Output [1, 3, 11, 5, 7, 9]Algorithm to insert a node at the end of the linked list
Method to insert a node at the end of the linked list#Insertion at the end in a Singly Linked List #Function to add node in the end def atEnd(self, value): new_node = Node(value) if self.head is None: self.head = new_node self.tail = new_node return last_node = self.head while(last_node.next): last_node = last_node.next last_node.next=new_node self.tail = new_node #Initially, we have a linked list (1,3,5,7,9) called "sll". sll.atEnd(0) print([node.value for node in sll]) #Output [1, 3, 5, 7, 9, 0]Time and Space ComplexityIn python, instead of iterating over the linked list and reaching the required position, we can directly jump to any point. Therefore, the time complexity of insertion in a singly linked list is O(1). But, for insertion at the end, the time complexity is O(N) as we need to traverse to the last element. The space complexity is O(1) because it takes a constant space to add a new node. Linked Lists in Python: An Introductionby Pedro Pregueiro intermediate python Mark as Completed Tweet Share Email Table of Contents Remove adsWatch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Working With Linked Lists in Python Linked lists are like a lesser-known cousin of lists. They’re not as popular or as cool, and you might not even remember them from your algorithms class. But in the right context, they can really shine. In this article, you’ll learn:
If you’re looking to brush up on your coding skills for a job interview, or if you want to learn more about Python data structures besides the usual dictionaries and lists, then you’ve come to the right place! You can follow along with the examples in this tutorial by downloading the source code available at the link below: Get the Source Code: Click here to get the source code you’ll use to learn about linked lists in this tutorial. Make a loop at k-th position in a linked list
Given a linked list and a position k. Make a loop at k-th position Recommended: Please try your approach on {IDE} first, before moving on to the solution. Algorithm C++
Java
Python3
C#
Javascript
Output: Given list 1 2 3 4 5 6 7 Modified list 1 2 3 4 5 6 7 ?list=PLqM7alHXFySH41ZxzrPNj2pAYPOI8ITe7 Article Tags : Linked List Practice Tags : Linked List |