Skip to content

Commit

Permalink
Linkedlist problems with sub class and parent class
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0910 committed Mar 3, 2020
1 parent f368b1f commit c579232
Show file tree
Hide file tree
Showing 10 changed files with 604 additions and 495 deletions.
459 changes: 0 additions & 459 deletions linkedlists/singly_linked_list.py

This file was deleted.

89 changes: 89 additions & 0 deletions linkedlists/singly_linked_list/DutchNationalFlagProblem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from singly_linked_list import LinkedList


class DNFLinkedList(LinkedList):


def dutch_national_flag_problem(self):
"""
Dutch national flag problem to separate 0's, 1's, and 2's in a linked list.
:return: None
"""
zero_start = zero_end = None
one_start = one_end = None
two_start = two_end = None
temp = self.head
while temp:
if temp.data == 0:
if not zero_start:
zero_start = temp
zero_end = temp
else:
zero_end.next = temp
zero_end = temp
if temp.data == 1:
if not one_start:
one_start = temp
one_end = temp
else:
one_end.next = temp
one_end = temp
if temp.data == 2:
if not two_start:
two_start = temp
two_end = temp
else:
two_end.next = temp
two_end = temp
temp = temp.next
zero_end.next = one_start
one_end.next = two_start
two_end.next = None
self.head = zero_start

def separate_even_odd_numbers(self):
"""
Function to separate event and odd numbers in a linked list
:return: None
"""
even_start = even_end = None
odd_start = odd_end = None
temp = self.head
while temp:
if temp.data % 2 == 0:
if not even_start:
even_start = temp
else:
even_end.next = temp
even_end = temp
if temp.data % 2 == 1:
if not odd_start:
odd_start = temp
else:
odd_end.next = temp
odd_end = temp
temp = temp.next
even_end.next = odd_start
odd_end.next = None
self.head = even_start


if __name__ == "__main__":
ll = DNFLinkedList()
ll.insert(0)
ll.insert(1)
ll.insert(2)
ll.insert(0)
ll.insert(1)
ll.insert(2)
ll.insert(0)
ll.insert(1)
ll.insert(2)
ll.insert(1)
ll.insert(2)
ll.insert(0)
ll.print_linked_list()
ll.dutch_national_flag_problem()
ll.print_linked_list()
ll.separate_even_odd_numbers()
ll.print_linked_list()
54 changes: 54 additions & 0 deletions linkedlists/singly_linked_list/add_two_numbers_in_linkedlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from singly_linked_list import LinkedList, Node


class AddNumbers(LinkedList):

@staticmethod
def add_two_number(l1, l2):
"""
Function to add two numbers represented by linked list by a single traversal.
:param l1: Node
:param l2: Node
:return: Node
"""
num1 = l1
num2 = l2
carry = 0
l3 = None
l3_tail = None

while num1 or num2:
total = (num1.data if num1 else 0) + (num2.data if num2 else 0) + carry
carry = total // 10
total = total % 10
node = Node(total)
if not l3:
l3 = node
else:
l3_tail.next = node
l3_tail = node
if num1:
num1 = num1.next
if num2:
num2 = num2.next
if carry != 0:
l3.insert(carry)
return l3


if __name__ == "__main__":
ll1 = AddNumbers()
ll1.insert(5)
ll1.insert(2)
ll1.insert(5)
print("Number 1", end=" - ")
ll1.print_linked_list()
ll2 = AddNumbers()
ll2.insert(4)
ll2.insert(2)
print("Number 2", end=" - ")
ll2.print_linked_list()
ll3 = AddNumbers()
ll3.head = AddNumbers.add_two_number(ll1.head, ll2.head)
print("Sum of numbers - ", end=" ")
ll3.print_linked_list()
90 changes: 90 additions & 0 deletions linkedlists/singly_linked_list/check_palindrome_in_linkedlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from singly_linked_list import LinkedList
from reverse_linkedlist import LinkedListReverse


class CheckPalindrome(LinkedList):

def check_palindrome_using_stack(self):
"""
Function to check whether elements in linked list form a palindrome of not.
It traverses the linked list twice, once to push all elements in stack and other to verify elements in the stack
:return: Bool
"""
stack = []
temp = self.head
while temp:
stack.append(temp.data)
temp = temp.next
temp = self.head
while temp:
if temp.data != stack[0]:
return False
stack.pop(0)
temp = temp.next
return True

@staticmethod
def compare_list(first, second):
"""
Function to compare elements of the two list
:param first: Node
:param second: Node
:return: Bool
"""
temp1 = first
temp2 = second
while temp1 and temp2:
if temp1.data == temp2.data:
temp1 = temp1.next
temp2 = temp2.next
else:
return False
if not temp1 and not temp2:
return True
return False

def check_palindrome_using_reverse(self):
"""
Function to check whether elements in the liked list form a palindrome or not.
This function gets to the middle of linked list and reverses the second half to check it it matches with the
first half. It reverses the second half again to form the second half
:return: Bool
"""
slow = self.head
fast = self.head
midnode = None
prev_to_slow = None
while fast and fast.next:
prev_to_slow = slow
slow = slow.next
fast = fast.next.next
if fast:
midnode = slow
slow = slow.next
prev_to_slow.next = None
second_half = slow
second_half = LinkedListReverse.iterative_reverse(second_half)
res = CheckPalindrome.compare_list(self.head, second_half)
second_half = LinkedListReverse.iterative_reverse(second_half)
if midnode:
prev_to_slow.next = midnode
midnode.next = second_half
else:
prev_to_slow.next = second_half
return res


if __name__ == "__main__":
ll = CheckPalindrome()
ll.insert(5)
ll.insert(4)
ll.insert(3)
ll.insert(4)
ll.insert(5)
ll.print_linked_list()
x = ll.check_palindrome_using_stack()
print(x)
ll.insert(6)
ll.print_linked_list()
x = ll.check_palindrome_using_reverse()
print(x)
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
class Node:
"""
Linked list node
"""
def __init__(self, data):
self.data = data
self.next = None
from singly_linked_list import LinkedList


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

def add(self, value):
node = Node(value)
node.next = self.head
self.head = node

def print(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
class DecimalEquivalent(LinkedList):

def decimal_equivalent(self):
"""
Expand All @@ -44,16 +23,19 @@ def decimal_equivalent(self):
return decimal


ll = LinkedList()
ll.add(0)
ll.add(0)
ll.add(1)
# ll.add(0)
# ll.add(1)
# ll.add(1)
# ll.add(0)
# ll.add(0)
# ll.add(0)
ll.print()
x = ll.decimal_equivalent()
print(x)
if __name__ == "__main__":
ll1 = DecimalEquivalent()
ll1.insert(0)
ll1.insert(0)
ll1.insert(1)
ll1.print_linked_list()
x = ll1.decimal_equivalent()
print("Decimal equivalent of above list is {}".format(x))
ll2 = DecimalEquivalent()
ll2.insert(0)
ll2.insert(1)
ll2.insert(1)
ll2.insert(1)
ll2.print_linked_list()
x = ll2.decimal_equivalent()
print("Decimal equivalent of above list is {}".format(x))
File renamed without changes.
35 changes: 35 additions & 0 deletions linkedlists/singly_linked_list/k_nodes_reverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from singly_linked_list import LinkedList

class KNodesReverse(LinkedList):

def k_nodes_reverse(self, head, k):
"""
Iterative function to perform K node reverse on a linked list.
:param head: Node
:param k: Int
:return: Node
"""
curr = head
prev = None
count = 0
while curr and count < k:
count += 1
next = curr.next
curr.next = prev
prev = curr
curr = next
if curr:
head.next = self.k_nodes_reverse(curr, k)
return prev


ll = KNodesReverse()
ll.insert(6)
ll.insert(5)
ll.insert(4)
ll.insert(3)
ll.insert(2)
ll.insert(1)
ll.print_linked_list()
ll.head = ll.k_nodes_reverse(ll.head, 2)
ll.print_linked_list()
Loading

0 comments on commit c579232

Please sign in to comment.