0% found this document useful (0 votes)
16 views28 pages

Sorting Techniques: Bubble Sort (Slowest)

The document provides an overview of various sorting techniques including Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Quick Sort, and Counting Sort, along with their implementations in Python. It also covers the implementation of Singly Linked Lists, including methods for appending, inserting, displaying, searching, merging, and sorting nodes. Additionally, it includes a Stack implementation using a linked list with methods for pushing and popping elements.

Uploaded by

shreyasdure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views28 pages

Sorting Techniques: Bubble Sort (Slowest)

The document provides an overview of various sorting techniques including Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Quick Sort, and Counting Sort, along with their implementations in Python. It also covers the implementation of Singly Linked Lists, including methods for appending, inserting, displaying, searching, merging, and sorting nodes. Additionally, it includes a Stack implementation using a linked list with methods for pushing and popping elements.

Uploaded by

shreyasdure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

 Sorting Techniques

Bubble Sort (slowest)

def bubbleSort(arr):
l = len(arr)
for i in range(l-1):
swapped = False
for j in range(l-i-1):
if arr[j]>arr[j+1]: #change the comparision operator to < for decending order
arr[j],arr[j+1]= arr[j+1],arr[j]
swapped = True
if not swapped:
return arr

return arr

print(bubbleSort([2,5,4,1,9,7]))

[1, 2, 4, 5, 7, 9]

Insertion Sort

def insertionSort(arr):
l = len(arr)
for i in range(1,l):
x = arr[i]
for j in range(i):
if x<arr[j]:
for k in range(i,j,-1):
arr[k]=arr[k-1]
arr[j] = x

1 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

break
return arr

print(insertionSort([-1,2,5,4,0,1,-1,9,7]))

[-1, -1, 0, 1, 2, 4, 5, 7, 9]

Selection Sort

def selectionSort(arr):
l = len(arr)
for i in range(l):
min = i
for j in range(i+1,l):
if arr[min]>arr[j]:
min = j
if i !=min:
arr[i],arr[min] = arr[min],arr[i]
return arr

print(selectionSort([-1,2,5,4,0,1,-1,9,7]))

[-1, -1, 0, 1, 2, 4, 5, 7, 9]

Merge Sort (Fast)

def mergeTwoSortedArrays(nums1,nums2):
i = 0
j = 0
right = nums1[i]
left = nums2[j]
x = []
while i<len(nums1) and j<len(nums2):
right = nums1[i]
left = nums2[j]
if right<left

2 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

if right<left:
x.append(right)
i+=1
else:
x.append(left)
j+=1
if j<len(nums2):
for i in range(j,len(nums2)):
x.append(nums2[i])
elif i<len(nums1):
for j in range(i,len(nums1)):
x.append(nums1[j])
return x

def mergeSort(arr):
mid = len(arr)//2
arr1 = arr[:mid]
arr2 = arr[mid:]
if len(arr1)>1:
arr1 = mergeSort(arr1)
if len(arr2)>1:
arr2 = mergeSort(arr2)
return mergeTwoSortedArrays(arr1,arr2)

print(mergeSort([-1,2,5,4,0,1,-1,9,7]))

[-1, -1, 0, 1, 2, 4, 5, 7, 9]

Quick Sort (Fast)

def quickSort(arr):
x = [arr[-1]]
left = [arr[i] for i in range(len(arr)-1) if arr[i]<=x[0]]
right = [arr[i] for i in range(len(arr)-1) if arr[i]>x[0]]

if len(left)>1:

3 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

if len(left)>1:
left = quickSort(left)
if len(right)>1:
right = quickSort(right)

return left+x+right

print(quickSort([-1,2,5,4,0,1,-1,9,7]))

[-1, -1, 0, 1, 2, 4, 5, 7, 9]

Counting Sort (Only for small non negative values)

#from gpt as it's too easy I don't wanna try it out


def countingSort(arr):
if not arr:
return []

max_val = max(arr)
count = [0] * (max_val + 1)

for num in arr:


count[num] += 1

sorted_arr = []
for i in range(len(count)):
sorted_arr.extend([i] * count[i])

return sorted_arr

print(countingSort([4, 2, 2, 8, 3, 3,1]))

# [1,100000000000,2] RAM will surely sucide


# [-1,1,2,1] Error

[1, 2, 2, 3, 3, 4, 8]

4 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

 UNIT 3 Linked Lists

Singly Linked Lists

import math,random

#Node in a singly Linked List


class node:
def __init__(self,data=None):
self.data = data
self.next = None

class linkedList:
def __init__(self):
self.head = None

def append(self,data):
if self.head == None:
self.head = node(data)
else:
temp = self.head
while temp and temp.next:
temp = temp.next
temp.next = node(data)
def insert(self,pos,data):
if pos == 0:
temp = node(data)
temp.next = self.head
self.head = temp
else:
temp = self.head
for i in range(pos-1):
temp = temp.next

5 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

temp = temp.next
if not temp:
return None
newNode = node(data)
newNode.next = temp.next
temp.next = newNode
def display(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
def length(self):
temp = self.head
i = 0
while temp:
i+=1
temp = temp.next

return i

def search(self,data):
temp = self.head
i = 0
while temp:
if temp.data == data:
return i
temp = temp.next
i+=1

return None

def merge(self,list2,list1=None):
if not list1:
list1= self.head
if not list1:
return list2
if not list2:
list1

6 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

return list1

main = list1 if list1.data<=list2.data else list2


other = list2 if main == list1 else list1
res = main
while main and main.next and other:
if main.next.data>=other.data:
temp = main.next
main.next = other
other = other.next
main = main.next
main.next = temp
else:
main = main.next
if other:
main.next = other
self.head = res
return res

def sorted(self,x):
if not x or not x.next:
return x
left = x
slow = x
fast = x
pre = None
while fast and fast.next:
fast = fast.next.next
pre = slow
slow = slow.next
if pre:
pre.next = None
right = slow
if left:
left = self.sorted(left)
if right:
right = self.sorted(right)

7 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

return self.merge(left,right)
def sort(self):
self.head = self.sorted(self.head)

def remove(self,data):
if not self.head:
return None
if self.head.data == data:
self.head = self.head.next
else:
pre = self.head
temp = self.head.next
while temp:
if temp.data == data:
break
pre = temp
temp = temp.next

if temp:
pre.next = temp.next

def pop(self,index=None):
if not self.head:
return None
if index == 0:
self.head = self.head.next
elif index == None:
if not self.head.next:
self.head = None
return
pre = self.head
temp = self.head.next
while temp and temp.next:
pre = temp
temp = temp.next

pre.next = None
else:

8 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

pre = self.head
if not self.head.next:
return None
temp = self.head.next
for i in range(index - 1):
if not temp:
return None
pre = temp
temp = temp.next
if temp:
pre.next = temp.next
def concat(self,other):
temp = self.head
while temp and temp.next:
temp = temp.next
temp.next = other.head

def reverse(self):
if not self.head or not self.head.next:
return
temp = self.head
prev = None
while temp.next:
next = temp.next
temp.next = prev
prev = temp
temp = next
temp.next = prev
self.head = temp

ll1 = linkedList()
for val in [10, 20, 30, 40]: ll1.append(val)
ll1.display()

ll1.insert(0, 5) # Insert at head


ll1.insert(2, 15) # Insert at pos 2
ll1.insert(ll1.length(), 50) # Insert at end
ll1.display()

9 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

ll1.display()

print("Search 15:", ll1.search(15))


print("Search 99:", ll1.search(99))

ll1.remove(20) # Remove 20
ll1.pop() # Pop last
ll1.pop(2) # Pop at index 2
ll1.display()

ll1.reverse()
ll1.display()

# Concat with another list


ll2 = linkedList()
for val in [100, 200, 300]: ll2.append(val)
ll1.concat(ll2)
ll1.display()

# Merge two sorted lists


s1, s2 = linkedList(), linkedList()
for val in [1, 3, 5]: s1.append(val)
for val in [2, 4, 6]: s2.append(val)
s1.merge(s2.head) # assuming merge needs node, not linkedlist
s1.display()

ll2 = linkedList()
for i in range(10):
ll2.append(random.randint(0,10))
ll2.display()
ll2.sort()
ll2.display()

10 20 30 40
5 10 15 20 30 40 50
Search 15: 2
Search 99: None

10 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

Search 99: None


5 10 30 40
40 30 10 5
40 30 10 5 100 200 300
1 2 3 4 5 6
3 9 9 7 6 6 2 1 6 8
1 2 3 6 6 6 7 8 9 9

Stack Implementation using Linked List

class Stack:
def __init__(self):
self.head = None
def append(self,data):
if self.head == None:
self.head = node(data)
else:
temp = self.head
while temp and temp.next:
temp = temp.next

temp.next = node(data)
def pop(self):
if not self.head:
return
if not self.head.next:
self.head = None
else:
temp = self.head
while temp and temp.next and temp.next.next:
temp = temp.next
temp.next = None
def display(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
print()

11 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

print()

s = Stack()

print("Initial stack (should be empty):")


s.display()

# Push elements
s.append(10)
s.append(20)
s.append(30)
s.append(40)

print("Stack after pushing 10, 20, 30, 40:")


s.display()

# Pop once
s.pop()
print("Stack after popping once (should remove 40):")
s.display()

# Pop again
s.pop()
print("Stack after popping again (should remove 30):")
s.display()

# Pop until empty


s.pop()
s.pop()
print("Stack after popping all elements (should be empty):")
s.display()

# Pop on empty (edge case!)


print("Popping from empty stack (should not crash):")
s.pop()
s.display()

12 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

s.display()

Initial stack (should be empty):

Stack after pushing 10, 20, 30, 40:


10 20 30 40
Stack after popping once (should remove 40):
10 20 30
Stack after popping again (should remove 30):
10 20
Stack after popping all elements (should be empty):

Popping from empty stack (should not crash):

Queue Implementation using Linked List

class Queue:
def __init__(self):
self.head = None
def append(self,data):
if self.head == None:
self.head = node(data)
else:
temp = self.head
while temp and temp.next:
temp = temp.next

temp.next = node(data)
def pop(self):
if not self.head:
return
if not self.head.next:
self.head = None
else:
self.head = self.head.next
def display(self):
temp = self.head

13 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

while temp:
print(temp.data, end=" ")
temp = temp.next
print()

# Assuming node class is already defined:


class node:
def __init__(self, data):
self.data = data
self.next = None

# Initialize the Queue


q = Queue()

# Initial state (empty)


print("Initial queue (should be empty):")
q.display()

# Append elements to the queue


q.append(10)
q.append(20)
q.append(30)
q.append(40)

print("Queue after appending 10, 20, 30, 40:")


q.display()

# Pop the front element (10)


q.pop()
print("Queue after popping the front (should remove 10):")
q.display()

# Pop another element (20)


q.pop()
print("Queue after popping again (should remove 20):")
q.display()

# Pop until empty

14 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

# Pop until empty


q.pop()
q.pop()
print("Queue after popping all elements (should be empty):")
q.display()

# Pop from an empty queue (edge case)


print("Popping from empty queue (should not crash):")
q.pop()
q.display()

Initial queue (should be empty):

Queue after appending 10, 20, 30, 40:


10 20 30 40
Queue after popping the front (should remove 10):
20 30 40
Queue after popping again (should remove 20):
30 40
Queue after popping all elements (should be empty):

Popping from empty queue (should not crash):

Doubly Linked List

class node:
def __init__(self,data=None):
self.data = data
self.next = None
self.prev = None

class doublyLinkedList:
def __init__(self):
self.head = None
def append(self,data):

15 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

if not self.head:
self.head = node(data)
else:
temp = self.head
while temp.next:
temp = temp.next
newNode = node(data)
newNode.prev = temp
temp.next = newNode
def display(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
print()

def pop(self,index=None):
if index == 0 and self.head:
self.head=self.head.next
if self.head:
self.head.prev = None

elif index == None:


if self.head and self.head.next == None:
self.head = None
else:
pre = None
temp = self.head
while temp and temp.next:
pre = temp
temp = temp.next
if pre:
pre.next = None
else:
self.head = None
else:
pre = None
temp = self.head

16 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

for i in range(index-1):
if not temp:
break
pre = temp
temp = temp.next

if temp:
pre.next = temp.next
if temp.next:
temp.next.prev = pre
def insert(self,data,index=None):
newNode = node(data)
if index == 0:
if not self.head:
self.head = newNode
else:
newNode.next = self.head
self.head.prev = newNode
self.head = newNode
elif index is not None:
pre = None
temp = self.head
for i in range(index):
if not temp:
break
pre = temp
temp = temp.next

if pre:
newNode.next = pre.next
pre.next = newNode
newNode.prev = pre
if temp:
temp.prev = newNode
def length(self):
temp = self.head
i = 0
while temp:

17 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

temp
i+=1
temp = temp.next

return i

def search(self,data):
temp = self.head
i = 0
while temp:
if temp.data == data:
return i
temp = temp.next
i+=1

return None

def remove(self,data):
temp = self.head
pre = None
while temp:
if temp.data == data:
break
pre = temp
temp = temp.next

if temp:
if pre:
pre.next = pre.next.next
if pre.next:
pre.next.prev = pre
else:
self.head = self.head.next
if self.head:
self.head.prev = None

dll = doublyLinkedList()

18 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

# Append elements
dll.append(10)
dll.append(20)
dll.append(30)

# Display list
print("List after appending 10, 20, 30:")
dll.display() # Expected: 10 20 30

# Insert element at the beginning


dll.insert(5, 0)
print("List after inserting 5 at the beginning:")
dll.display() # Expected: 5 10 20 30

# Insert element at index 2


dll.insert(15, 2)
print("List after inserting 15 at index 2:")
dll.display() # Expected: 5 10 15 20 30

# Pop the last element


dll.pop()
print("List after popping the last element:")
dll.display() # Expected: 5 10 15 20

# Pop the first element (index 0)


dll.pop(0)
print("List after popping the first element:")
dll.display() # Expected: 10 15 20

# Remove element by value (20)


dll.remove(20)
print("List after removing 20:")
dll.display() # Expected: 10 15

# Search for element (15)


print("Search for 15:", dll.search(15)) # Expected: 1 (index of 15)

19 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

# Check length
print("Length of list:", dll.length()) # Expected: 2

List after appending 10, 20, 30:


10 20 30
List after inserting 5 at the beginning:
5 10 20 30
List after inserting 15 at index 2:
5 10 15 20 30
List after popping the last element:
5 10 15 20
List after popping the first element:
10 15 20
List after removing 20:
10 15
Search for 15: 1
Length of list: 2

Circular Linked list

class node:
def __init__(self,data=None) -> None:
self.data = data
self.next = None

class circularLinkedList:
def __init__(self) -> None:
self.head = None
self.tail = None

def append(self,data):
newNode = node(data)
if not self.head:
self.head = newNode
self.tail = self.head
self.tail.next == self.head
else:

20 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

self.tail.next = newNode
newNode.next = self.head
self.tail = newNode

def display(self):
temp = self.head
while temp:
print(temp.data,end=" ")
if temp == self.tail:
break
temp = temp.next
print()

def insertAtBeginning(self,data):
newNode = node(data)
if not self.head:
self.head = newNode
self.tail = self.head
self.tail.next == self.head
else:
newNode.next = self.head
self.head = newNode
self.tail.next = self.head

def pop(self):
if not self.head:
return
if self.head == self.tail:
self.head = self.tail = None
else:
temp = self.head
while temp.next != self.tail:
temp = temp.next

temp.next = self.head
self.tail = temp

def deleteFromBeginning(self):

21 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

def deleteFromBeginning(self):
if not self.head:
return
if self.head == self.tail:
self.head = self.tail = None
else:
self.head = self.head.next
self.tail.next = self.head
def search(self,data):
temp = self.head
i = 0
while temp:
if temp.data==data:
print(i)
if temp == self.tail:
break
temp = temp.next
i+=1

cll = circularLinkedList()
for val in [10, 20, 30]: cll.append(val)
cll.search(20)
for val in [5, 2]: cll.insertAtBeginning(val)
cll.display()
for _ in range(2): cll.pop()
cll.display()
for _ in range(2): cll.deleteFromBeginning()
cll.display()

1
2 5 10 20 30
2 5 10
10

Addition of long positive integers Using Linked Lists

22 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

class node:
def __init__(self,data=None) -> None:
self.data = data
self.next = None

class number:
def __init__(self,data):
temp = None
for i in data:
newNode = node(int(i))
newNode.next = temp
temp = newNode
self.head = newNode
temp = self.head
while temp:
temp = temp.next

def add(self,other):
other = other.head #You're passing other as a number object, but treating it like a node.
sum = 0
carry = 0
temp = self.head
while temp or other:
a=b=0
if other:
a = other.data
other = other.next
if temp:
b = temp.data
temp = temp.next
s = a+b+carry
sum*=10
sum+=(s)%10
carry = s//10
if carry:
sum*=10
sum+=carry

23 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

print(sum)

num1 = "987654321987654321987654321987654321987654321987654321"
num2 = "123456789123456789123456789123456789123456789123456789"

n1 = number(num1)
n2 = number(num2)

n1.add(n2) #1111111111111111111111111111111111111111111111111111110

111111111111111111111111111111111111111111111111111111

Adding Polynomials

Given two polynomials represented as linked lists sorted by decreasing exponent order, add them and return the resulting
polynomial as a new linked list

class node:
def __init__(self,coef=1,exp=1) -> None:
self.coef = coef
self.exp = exp
self.next = None

class polynomial:
def __init__(self):
self.head = None
self.superscript_map = {
'0': '⁰', '1': '¹', '2': '²', '3': '³', '4': '⁴', '5': '⁵', '6': '⁶', '7': '⁷', '8': '⁸', '9': '⁹',
'+': '⁺', '-': '⁻', '=': '⁼', 'a': 'ᵃ', 'b': 'ᵇ', 'c': 'ᶜ', 'd': 'ᵈ', 'e': 'ᵉ', 'f': 'ᶠ', 'g': 'ᵍ',
'h': 'ʰ', 'i': 'ⁱ', 'j': 'ʲ', 'k': 'ᵏ', 'l': 'ˡ', 'm': 'ᵐ', 'n': 'ⁿ', 'o': 'ᵒ', 'p': 'ᵖ', 'q': 'ᵠ',
'r': 'ʳ', 's': 'ˢ', 't': 'ᵗ', 'u': 'ᵘ', 'v': 'ᵛ', 'w': 'ʷ', 'x': 'ˣ', 'y': 'ʸ', 'z': 'ᶻ'}

24 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

def append(self,coef=1,exp=1):
if coef == 0:
return
if not self.head:
self.head = node(coef,exp)
else:
temp = self.head
flag = True
if exp<self.head.exp:
newNode = node(coef,exp)
newNode.next = self.head
self.head = newNode
flag = False
while temp.next and flag:
if exp==temp.exp:
flag = False
temp.coef+=coef
break
if exp<temp.next.exp:
newNode = node(coef,exp)
newNode.next = temp.next
temp.next = newNode
flag = False
break
temp = temp.next
if exp==temp.exp and flag:
flag = False
temp.coef+=coef
if flag:
temp.next = node(coef,exp)

def add(self,other):
other = other.head
while other:
self.append(other.coef,other.exp) #EASY APPROACH
other = other.next

temp = self.head

25 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

if other and other.exp<self.head.exp:


node = None
while other and other.exp<temp.exp:
newNode = node(other.coef, other.exp)
if node:
node.next = newNode
node = newNode
else:
self.head = newNode
node = newNode
other = other.next

node.next = temp

temp = node

while temp.next and other:


if other and other.exp==temp.exp:
temp.coef+=other.coef
other = other.next
temp = temp.next
continue
if other and other.exp<temp.next.exp:
node = other
other = other.next
node.next = temp.next
temp.next = node
continue
temp = temp.next

if other:
temp.next = other

def display(self):
temp = self.head
output = ""
while temp:

26 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

temp
x = ""
if temp.coef == 0:
temp = temp.next
continue
if temp.coef>0:
if temp.next:
x+="+"
x+=str(temp.coef)
if temp.exp != 0:
if temp.exp>9:
k = str(temp.exp)
power = ""
for i in k:
power+=self.superscript_map[i]
x+="x"+power
else:
x+=f"x{self.superscript_map[str(temp.exp)]}"
x+=" "
output= x+output
temp = temp.next
print(output)

poly1 = polynomial()
poly1.append(2, 1)

poly2 = polynomial()
poly2.append(1, 0)
poly2.append(3, 2)

poly1.add(poly2)
poly1.display()

3x² +2x¹ +1

27 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...

28 of 28 4/28/2025, 9:58 PM

You might also like