def BuildListOfDepths(self): root = self.binaryTreeRoot if root is None: return current = LinkedList() current.append(root) while current.getListSize() > 0: self.allListsOfDepths.append(current) # add the previous level parents = current # go to next level current = LinkedList() currentParent = parents.head # head of the linked list (holds a binary node) while currentParent is not None: node = currentParent.data if node.leftNode is not None: current.append(node.leftNode) if node.rightNode is not None: current.append(node.rightNode) currentParent = currentParent.nextNode
from Chapter_2.LinkedList import * llist = LinkedList() llist.append(1) llist.append(1) llist.append(1) llist.append(1) llist.append(2) llist.append(2) llist.append(3) llist.append(3) llist.append(3) llist.append(3) llist.append(4) llist.append(4) llist.append(4) llist.append(4) llist.append(4) llist.append(4) llist.append(4) llist.append(5) llist.append(5) llist.append(5) llist.append(5) llist.append(5)
def PrintSummedListResult(self, result): sumedListResult = LinkedList() resultAsAstr = list(str(result)) for result in resultAsAstr: sumedListResult.append(result) sumedListResult.printList()
def GetSummedListsNumber(self, list1, list2): currentNode_1 = list1.head currentNode_2 = list2.head acumulate = 0 while currentNode_1 is not None: nodesSum = int(currentNode_1.data) + int(currentNode_2.data) currentNode_1 = currentNode_1.nextNode currentNode_2 = currentNode_2.nextNode acumulate += nodesSum return acumulate def PrintSummedListResult(self, result): sumedListResult = LinkedList() resultAsAstr = list(str(result)) for result in resultAsAstr: sumedListResult.append(result) sumedListResult.printList() list1 = LinkedList() list1.append(99) list1.append(2) list1.append(4) list2 = LinkedList() list2.append(26) list2.append(3) list2.append(6) SumLinkedLists().PrepareListsToSum(list1, list2)
from Chapter_2.LinkedList import * llist = LinkedList() llist.append(1) llist.append(2) llist.append(3) llist.append(4) llist.append(5) llist.printList() llist.deleteMiddleNodeFromList(llist) llist.printList()
if list1_size > list2_size: difference = list1_size - list2_size self.removeCharactersFromHeadOfLinkedList(difference, list1) if list2_size > list1_size: difference = list2_size - list1_size self.removeCharactersFromHeadOfLinkedList(difference, list2) def removeCharactersFromHeadOfLinkedList(self, howManyCharsToRemove, thisList): for i in range(howManyCharsToRemove): thisList.deleteNode("index", 0) llist_1 = LinkedList() llist_1.append(1) llist_1.append(2) llist_1.append(45) llist_1.append(532) llist_1.printList() llist_2 = LinkedList() llist_2.append(13) llist_2.append(27) llist_2.append(555) llist_2.append(4) llist_2.append(5) llist_2.append(45) llist_2.append(532) llist_2.printList()
if slow == fast: # collision break # Error checking - no meeting point, and therefore no loop if fast is None or fast.nextNode is None: return None ''' Move slow to head. Keep fast at Meeting point. Each are k steps from the Loop Start. If they move at the same pace, they must meet at Loop Start. ''' slow = linkedList.head while slow != fast: slow = slow.nextNode fast = fast.nextNode # Both now point to the start of the loop return fast llist = LinkedList() llist.append("A") llist.append("B") llist.append("C") llist.append("D") llist.append("E") llist.append("C") llist.printList() result = LinkedListLoopDetect().FindBeginning(llist) print(result)