Implement a simple linked list in Python (Write source code and show output) with basic linked list operations like: (a) create a sequence of nodes and construct a linear linked list. (b) insert a new node in the linked list. (b) delete a particular node in the linked list. (c) modify the linear linked list into a circular linked list. Use this template: class Node: def __init__(self, val=None): self.val = val self.next = None class LinkedList: """ TODO: Remove the "pass" statements and implement each method Add any methods if necessary DON'T use a builtin list to keep all your nodes. """ def __init__(self): self.head = None # The head of your list, don't change its name. It should be "None" when the list is empty. def append(self, num): # append num to the tail of the list pass def insert(self, index, num): # insert num into the given index pass def delete(self, index): # remove the node at the given index and return the deleted value as an integer pass def circularize(self): # Make your list circular. This method can only be the last call of the autograder. pass if __name__ == "__main__": my_list = LinkedList() # [] my_list.insert(0, 32) # [32] my_list.append(-5) # [32, -5] my_list.append(19) # [32, -5, 19] my_list.insert(1, 6) # [32, 6, -5, 19] my_list.delete(2) # [32, 6, 19] my_list.circularize()
Implement a simple linked list in Python (Write source code and show output) with basic linked list operations like:
(a) create a sequence of nodes and construct a linear linked list.
(b) insert a new node in the linked list.
(b) delete a particular node in the linked list.
(c) modify the linear linked list into a circular linked list.
Use this template:
class Node:
def __init__(self, val=None):
self.val = val
self.next = None
class LinkedList:
"""
TODO: Remove the "pass" statements and implement each method
Add any methods if necessary
DON'T use a builtin list to keep all your nodes.
"""
def __init__(self):
self.head = None # The head of your list, don't change its name. It should
be "None" when the list is empty.
def append(self, num): # append num to the tail of the list
pass
def insert(self, index, num): # insert num into the given index
pass
def delete(self, index): # remove the node at the given index and return the
deleted value as an integer
pass
def circularize(self): # Make your list circular. This method can only be the
last call of the autograder.
pass
if __name__ == "__main__":
my_list = LinkedList() # []
my_list.insert(0, 32) # [32]
my_list.append(-5) # [32, -5]
my_list.append(19) # [32, -5, 19]
my_list.insert(1, 6) # [32, 6, -5, 19]
my_list.delete(2) # [32, 6, 19]
my_list.circularize()
![](/static/compass_v2/shared-icons/check-mark.png)
Algorithm to implement Linked list and perform some operations
- __init__(self): Initializes a new instance of the LinkedList class with an empty head node.
- Set the head node to None
- append(self, num): Adds a new node with the given num to the end of the linked list.
- Create a new node with the given num.
- If the head node is None, set the new node as the head node and return
- Traverse the linked list until the last node is reached 4. Set the next pointer of the last node to the new node
- insert(self, index, num): Inserts a new node with the given num at the specified index in the linked list.
- Create a new node with the given num
- If the index is 0, set the new node as the head node and return
- Traverse the linked list until the node at the (index - 1) position is reached
- Set the next pointer of the new node to the next node of the (index - 1) node
- Set the next pointer of the (index - 1) node to the new node
- delete(self, index): Removes the node at the specified index from the linked list and returns its value.
- If the index is 0, set the head node to the next node and return the value of the current head node
- Traverse the linked list until the node at the (index - 1) position is reached
- Set the next pointer of the (index - 1) node to the next node of the (index) node
- Return the value of the (index) node
- circularize(self): Changes the linked list into a circular linked list by setting the next pointer of the last node to the head node.
- If the head node is None, return
- Traverse the linked list until the last node is reached
- Set the next pointer of the last node to the head node
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)