Linked List Basics-1
Linked List Basics-1
1 LINKED LISTS
1.0.1 Singly Linked List
class LinkedList:
def __init__(self):
self.head = None
def http://localhost:8889/notebooks/Linked%20List%20Basics.
,→ipynb#insert_at_end(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node #if the node is first node
return
temp = self.head #Assign a temporary variable to traverse the list
while temp.next: #until temp.next!=None it will traverse the linked␣
,→list
temp = temp.next
temp.next = new_node
def display(self):
temp = self.head
while temp:
print(temp.data, end=" -> ")
temp = temp.next
print("None")
1
10 -> 20 -> None
return
temp = self.head
while temp.next: # Traverse to the last node
temp = temp.next
temp.next = new_node # Link the last node to the new node
2
if temp is None:
print(f"Element {key} not found in the list.")
return
# Insert elements
ll.insert_at_end(10)
ll.insert_at_end(20)
ll.insert_at_beginning(5)
ll.insert_at_end(30)
# Delete an element
ll.delete_node(20)
print("Linked List after deletion of 20:")
ll.traverse()
3
print(f"Element {element} found in the list.")
else:
print(f"Element {element} not found in the list.")
ll = LinkedList()
ll.insert_at_beginning(9)
ll.insert_at_beginning(5)
ll.insert_at_beginning(10)
4
[1]: #Adding two polynomials
# If the list is empty or the new term has a greater power than the head
if self.head is None or self.head.power < power:
new_node.next = self.head
self.head = new_node
return
temp = self.head
while temp.next and temp.next.power > power:
temp = temp.next
5
# Function to add two polynomials
@staticmethod
def add_polynomials(poly1, poly2):
p1 = poly1.head
p2 = poly2.head
result = PolynomialLinkedList()
return result
# Example usage
poly1 = PolynomialLinkedList()
poly1.insert_term(3, 2)
poly1.insert_term(5, 1)
poly1.insert_term(6, 0)
print("Polynomial 1:")
poly1.display()
poly2 = PolynomialLinkedList()
poly2.insert_term(4, 2)
poly2.insert_term(2, 1)
poly2.insert_term(8, 0)
6
print("Polynomial 2:")
poly2.display()
Polynomial 1:
3x^2 + 5x^1 + 6x^0
Polynomial 2:
4x^2 + 2x^1 + 8x^0
Sum of the two polynomials:
7x^2 + 7x^1 + 14x^0
[ ]: #Deletion
def delete_head(self):
if self.head is None:
print("List is empty.")
return
self.head = self.head.next # Move head to the next node
temp = self.head
while temp.next.next is not None:
temp = temp.next
7
self.head = self.head.next # Move head to the next node
return
# Traverse the list to find the node with the specified value
temp = self.head
while temp.next and temp.next.data != value:
temp = temp.next
class Node:
def __init__(self, data):
self.data = data # The value/data stored in the node
self.next = None # Pointer to the next node
self.prev = None # Pointer to the previous node
#insert in beginning
class DoublyLinkedList:
def __init__(self):
self.head = None
new_node.next = self.head
self.head = new_node
if self.head is None:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
8
last.next = new_node
new_node.prev = last
class SinglyLinkedList:
def __init__(self):
self.head = None # Initially, the list is empty
self.tail = None # Initially, there is no tail
def display(self):
current = self.head # Start from the head of the list
while current: # Traverse the list until current is None
print(current.data, end=" ") # Display the data of the current node
current = current.next # Move to the next node
print() # For a new line after displaying the list
# Example usage:
sll = SinglyLinkedList() # Create an instance of SinglyLinkedList
9
sll.addNode(30)
sll.addNode(40)
Original List:
10 20 30 40
Reversed List:
40 30 20 10
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
10
def display(self):
if not self.head:
print("List is empty")
return
temp = self.head
while True:
print(temp.data, end=" -> ")
temp = temp.next
if temp == self.head:
break
print("... (circular)")
# Example Usage
cll = CircularLinkedList()
[ ]:
11