The LinkedList module of the eduAlgo package contains ten classic LinkedList algorithms,
Module: list_algorithms()
Method : is_palindrome(head), is_palindrome_optimized(head)
Uses : To check if a Linked List is palindromic or not. "is_palindrome_optimized()" is the optimized version of
the same using another algorithmic approach unlike the normal one which uses stack.
Parameters : head - head node of the Linked List, this method returs a boolean variable.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
arr1 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
sol = ll.list_algorithms()
print(sol.is_palindrome(llist1.head))
Input:
1 2 2 2 1
Output:
True
Module: list_algorithms()
Method : removeElements(head,val)
Uses : To remove all the occurance of a specific element passed as an arguement "val".
Parameters : head - head node of the Linked List, val - value to remove all the occurances. This method returns a Linked List node obejct.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
arr1 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
num = int(input())
sol = ll.list_algorithms()
llist3 = ll.linkedlist()
llist3.head = sol.removeElements(llist1.head,num)
llist3.printLL()
Input:
1 2 3 4 5 6 6 2 2 4 9
6
Output:
1 2 3 4 5 2 2 4 9
Module: list_algorithms()
Method : delete_sorted_duplicates(head)
Uses : To remove all the duplicate occurance from a sorted Linked List
Parameters : head - head node of the Linked List. This method returns a Linked List node obejct.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
arr1 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
sol = ll.list_algorithms()
llist3 = ll.linkedlist()
llist3.head = sol.delete_sorted_duplicate(llist1.head)
llist3.printLL()
Input:
1 2 2 3 3 5 5 9 9
Output:
1 2 3 5 9
Module: list_algorithms()
Method : mergeTwoLists(head1,head2)
Uses : To merge two Linked Lists.
Parameters : head1 - head node of the first
Linked List. head2 - head node of the second Linked List.
This method returns the head of the merged linked list as a Linked List node obejct.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
llist2 = ll.linkedlist()
arr1 = list(map(int,input().split()))
arr2 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
for i in arr2:
llist2.append(i)
sol = ll.list_algorithms()
llist3 = ll.linkedlist()
llist3.head = sol.mergeTwoLists(llist1.head,llist2.head)
llist3.printLL()
Input:
1 2 3 2 1
4 5 6 8 9
Output:
1 2 3 2 1 4 5 6 8 9
Module: list_algorithms()
Method : reverse_linked_recursive(head),
reverse_linked_iterative(head)
Uses : To reverse a Linked Lists (recursive and iterative approach).
Parameters : head- head node of the first
Linked List. This method returns the head of the reversed linked list as a Linked List node obejct.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
arr1 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
sol = ll.list_algorithms()
llist3 = ll.linkedlist()
llist3.head = sol.reverse_linked_iterative(llist1.head)
llist3.printLL()
Input:
1 2 3 4 5
Output:
5 4 3 2 1
Module: list_algorithms()
Method : delete_node(head)
Uses : To delete a particular given node passed as a reference.
Parameters : head- head node of the first
Linked List. This method returns void.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
arr1 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
sol = ll.list_algorithms()
sol.delete_node(llist1.head)
llist1.printLL()
Input:
1 2 3 4 5 6
Output:
2 3 4 5 6
Module: list_algorithms()
Method : middleNode(head)
Uses : To get the middle node of a Linked List.
Parameters : head- head node of the first
Linked List. This method returns the middle node as a Linked List Object.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
arr1 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
sol = ll.list_algorithms()
print(sol.middleNode(llist1.head).data)
Input:
1 2 3 4 5 6
Output:
4
Module: list_algorithms()
Method : length(head)
Uses : To get the length of a Linked List.
Parameters : head- head node of the first
Linked List. This method returns the length of the Linked List as an integer.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
arr1 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
sol = ll.list_algorithms()
print(sol.length(llist1.head))
Input:
1 2 3 4 5 6
Output:
6
Module: list_algorithms()
Method : getDecimalValue(head)
Uses : To get the decimal value of a Binary Linked List
Parameters : head- head node of the
Linked List. This method returns the decimal of the Linked List as an integer.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
arr1 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
sol = ll.list_algorithms()
print(sol.getDecimalValue(llist1.head))
Input:
1 0 0 1 0 1
Output:
37
Module: list_algorithms()
Method : nextLargerNodes(head)
Uses : To get the just greated node of the Linked List elements
Parameters : head- head node of the
Linked List. This method returns a list having the
same size as that of the Linked List where the list holds the next
larger node for each Linked List elements.
from edualgo import LinkedList as ll
llist1 = ll.linkedlist()
arr1 = list(map(int,input().split()))
for i in arr1:
llist1.append(i)
sol = ll.list_algorithms()
print(sol.nextLargerNodes(llist1.head))
Input:
2 1 5
Output:
[5,5,0]
Do you want to contribute to our project ? We welcome every python lover to come forwards and have some fun with our source code. Steps to contibute:
Keep coding, keep loving python