Linked List Implementations

Last updated: 19th Aug, 2020

Linked List Module

The LinkedList module of the eduAlgo package contains ten classic LinkedList algorithms,

  • Palindromic Linked List -
  • Remove Linked List Elements -
  • Remove Duplicates From a Sorted List -
  • Merge Two Sorted List -
  • Reverse Linked List -
  • Delete Node In a Linked List -
  • Middle Of the Linked List -
  • Length of the Linked List -
  • Getting Decimal Value from Binary -
  • Next Larger Nodes -

Palindromic Linked List

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.

Python Code Example
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
      
Remove Linked List Elements

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.

Python Code Example
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 
      
Remove Duplicates From a Sorted List

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.

Python Code Example
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 
      
Merge Two Sorted Lists

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.

Python Code Example
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 
      
Reverse Linked Lists

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.

Python Code Example
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 
      
Delete Node In a Linked Lists

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.

Python Code Example
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 
      
Middle of the Linked Lists

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.

Python Code Example
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 
      
Length Of the Linked List

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.

Python Code Example
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 
      
Getting Decimal Value From Binary

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.

Python Code Example
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 
    
Next Larger Nodes

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.

Python Code Example
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] 
  

Found Some Bugs ?

Mail The Developer here - abhijittripathy99@gmail.com

Are you an ambitious Python Developer ?

Instance Theme

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:

  • Send an email to the developer here regarding your willingness to contribute to the project.
  • Have a read of our "How To Contribute" documentation here.
Keep coding, keep loving python