def isPalindrome(linkedList): stack = LinkedList() res = True slow = linkedList.head fast = linkedList.head count=0 while(fast!= None and fast.next !=None): count += 1 stack.push(Node(slow.data)) slow = slow.next fast = fast.next.next if(fast != None): slow = slow.next while(slow!= None): val1 = slow.data val2 = stack.pop() if(val1 != val2): res = False break slow = slow.next return res
def sumDigitsReverseOrder(l1, l2): node1 = l1.head node2 = l2.head count, carry = 0, 0 result = LinkedList() while (not (node1 == None and node2 == None)): val1 = node1.data if node1 else 0 val2 = node2.data if node2 else 0 sum = val1 + val2 + carry result.appendNode(Node(sum % 10)) carry = sum // 10 if (node1): node1 = node1.next if (node2): node2 = node2.next if (carry): result.appendNode(Node(carry)) return result
def sumDigitsForwardOrder(linkedList1, linkedList2): len1 = linkedList1.length() len2 = linkedList2.length() higherLength = 0 if (len1 > len2): padListWithZero(linkedList2, len1 - len2) higherLength = len1 elif (len2 > len1): padListWithZero(linkedList1, len2 - len1) higherLength = len2 result, carry = addDigits(linkedList1.head, linkedList2.head) if (carry): result.push(Node(carry)) return result
def addDigits(l1, l2): result, carry = None, 0 if (l1.next or l2.next): result, carry = addDigits(l1.next, l2.next) if (not result): result = LinkedList() sum = l1.data + l2.data + carry print(f"{l1.data} + {l2.data} + {carry} = {sum}") result.push(Node(sum % 10)) carry = sum // 10 return result, carry
break start = linkedList.head if isLoopPresent: while(start != slow): start = start.next slow = slow.next return slow else: return None linkedList = LinkedList() linkedList.appendNode(Node("A")) linkedList.appendNode(Node("B")) n1 = Node("C") linkedList.appendNode(n1) linkedList.appendNode(Node("D")) linkedList.appendNode(Node("E")) linkedList.appendNode(n1) print("Input: ") # linkedList.printList() # do not print list since it contains a loop -> program will stuck in infinite loop # A -> B -> C -> D -> E -> C print("Output: ") res = hasLoop(linkedList) if res:
previous = linkedList.head current = linkedList.head.next while (current != None): if (current.data < x): previous.next = current.next temp = current #can avoid creating temp variable but it makes it simpler to understand temp.next = linkedList.head linkedList.head = temp current = previous.next else: previous = previous.next current = current.next linkedList = LinkedList() linkedList.appendNode(Node(3)) linkedList.appendNode(Node(5)) linkedList.appendNode(Node(8)) linkedList.appendNode(Node(5)) linkedList.appendNode(Node(10)) linkedList.appendNode(Node(2)) linkedList.appendNode(Node(1)) x = 5 print("Input: ") linkedList.printList() print(f"x: {x}") print("Output: ") # deleteDups(linkedList.head)
while (node != None): sum += node.data * (10**count) node = node.next count += 1 return sum def sumDigitsReverseOrder(l1, l2): n1 = convertLinkedListToNumber(l1) n2 = convertLinkedListToNumber(l2) print(f"{n1} + {n2} = {n1 + n2}") linkedList1 = LinkedList() linkedList1.appendNode(Node(7)) linkedList1.appendNode(Node(1)) linkedList1.appendNode(Node(6)) linkedList2 = LinkedList() linkedList2.appendNode(Node(5)) linkedList2.appendNode(Node(9)) linkedList2.appendNode(Node(2)) print("Input: ") linkedList1.printList() linkedList2.printList() print("Output: ") sumDigitsReverseOrder(linkedList1, linkedList2)
def padListWithZero(linkedList, count): while (count > 0): linkedList.push(Node(0)) count -= 1
if (len1 > len2): padListWithZero(linkedList2, len1 - len2) higherLength = len1 elif (len2 > len1): padListWithZero(linkedList1, len2 - len1) higherLength = len2 result, carry = addDigits(linkedList1.head, linkedList2.head) if (carry): result.push(Node(carry)) return result linkedList1 = LinkedList() # 617 linkedList1.appendNode(Node(6)) linkedList1.appendNode(Node(1)) linkedList1.appendNode(Node(7)) # 9864 linkedList2 = LinkedList() linkedList2.appendNode(Node(9)) linkedList2.appendNode(Node(8)) linkedList2.appendNode(Node(6)) linkedList2.appendNode(Node(4)) print("Input: ") linkedList1.printList() linkedList2.printList() print("Output: ")
def kthToLast(head, k): currentNode = head referenceNode = head for i in range(1, k): referenceNode = referenceNode.next while (referenceNode != None and referenceNode.next != None): referenceNode = referenceNode.next currentNode = currentNode.next return currentNode.data linkedList = LinkedList() linkedList.appendNode(Node(1)) linkedList.appendNode(Node(2)) linkedList.appendNode(Node(3)) linkedList.appendNode(Node(4)) linkedList.appendNode(Node(5)) linkedList.appendNode(Node(6)) print("Input: ") linkedList.printList() print("Output: ") # deleteDups(linkedList.head) print(f"1st from last: {kthToLast(linkedList.head, 1)}") print(f"2nd from last: {kthToLast(linkedList.head, 2)}") print(f"3rd from last: {kthToLast(linkedList.head, 3)}")
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) from Util.Node import Node from Util.LinkedList import LinkedList def deleteMiddleNode(node): if(node == None or node.next == None): return False node.data = node.next.data node.next = node.next.next return True linkedList = LinkedList() linkedList.appendNode(Node(1)) testNode = Node(2) linkedList.appendNode(testNode) linkedList.appendNode(Node(3)) linkedList.appendNode(Node(4)) linkedList.appendNode(Node(5)) print("Input: ") linkedList.printList() print("Output: ") # deleteDups(linkedList.head) deleteMiddleNode(testNode) linkedList.printList()
fast = fast.next.next if(fast != None): slow = slow.next while(slow!= None): val1 = slow.data val2 = stack.pop() if(val1 != val2): res = False break slow = slow.next return res linkedList = LinkedList() linkedList.appendNode(Node(2)) linkedList.appendNode(Node(3)) linkedList.appendNode(Node(4)) linkedList.appendNode(Node(3)) linkedList.appendNode(Node(2)) print("Input: ") linkedList.printList() print("Output: ") print(f"Palindrome: {isPalindrome(linkedList)}")
node1 = node1.next elif (len2 > len1): for i in range(0, diff): node2 = node2.next while (node1 != None and node2 != None): if (node1 == node2): return node1 node1 = node1.next node2 = node2.next return None linkedList1 = LinkedList() linkedList1.appendNode(Node(3)) linkedList1.appendNode(Node(1)) linkedList1.appendNode(Node(5)) linkedList1.appendNode(Node(9)) linkedList2 = LinkedList() linkedList2.appendNode(Node(4)) linkedList2.appendNode(Node(6)) n1 = Node(7) linkedList1.appendNode(n1) linkedList2.appendNode(n1) n2 = Node(2) linkedList1.appendNode(n2) linkedList2.appendNode(n2)