def test3(): #scenario: End node is the intersection LLend1 = LinkedList() LLend2 = LinkedList() endNode = Node("end") for num in range(19): LLend1.push(num) LLend2.push(num) print(num) LLend1.pushNode(endNode) LLend2.pushNode(endNode) result = findIntersection(LLend1, LLend2) #count = 435 # length of both lists = 19 # This is the worst case, and the worst case of checking for the end node, does not stop with the check. print(result)
def test2(): #worst case: Loop node is at the end, pointing at the first node. LLisLoop2 = LinkedList() loopNode2 = Node("LOOP2") for i in range(20): if i == 19: LLisLoop2.pushNode(loopNode2) else: LLisLoop2.push(i) #set the loop begin = LLisLoop2.begin loopNode2.next = begin result = findLoop(LLisLoop2) #count = 18 print(result)
#chapter 2 problem 6 from ch2_SetUp import Node, LinkedList notPali1 = "abbcbbac" #even notPali2 = "acccb" #odd Pali1 = "abbbba" #odd Pali2 = "aabbaa" #even LLnot1 = LinkedList() LLnot2 = LinkedList() LLisPali1 = LinkedList() LLisPali2 = LinkedList() for char in notPali1: LLnot1.push(char) for char2 in notPali2: LLnot2.push(char2) for char3 in Pali1: LLisPali1.push(char3) for char4 in Pali2: LLisPali2.push(char4) def isPalindrome(linkedlist): forwardstring = '' backwardstring = '' node = linkedlist.begin while node: data = str(node.data) forwardstring += data backwardstring = data + backwardstring
#chapter 2 problem 3 from ch2_SetUp import Node, LinkedList LL = LinkedList() values = ['ted', 'dy', 'bear', 'stuffed', 'jalapenos'] for item in values: LL.push(item) def getMiddle(val): start = LL.begin while start: if start.data == val: return start start = start.next return None def removeMiddle(node): # In python a Garbage Collector will remove any items that have no reference to them # So once nothing points at that middle node, the garbage collectore picks it up and discards it. # A begin node, or an end node would cause problems because the values would have to be reset in the Linked List. # self.begin would have to be set to the nextnode # self.end would need to be set to the previous node # Because we only have access to this node, there isn't a way to reach these values or assign new pointers to them if node == None: print("Error, Value can not be None.") if node.next: nextnode = node.next newdata = nextnode.data
#chapter 2 problem 7 from ch2_SetUp import Node, LinkedList LL1 = LinkedList() LL2 = LinkedList() LL3 = LinkedList() for char in "LIST": LL3.push(char) for char2 in "0101linked": LL2.push(char2) for char3 in "LINK2D": LL1.push(char3) nextNode = LL3.begin LL1.pushNode(nextNode) LL2.pushNode(nextNode) #LL1.printList() #print('-' * 10) #LL2.printList() #worst case is no intersection LL4 = LinkedList() LL5 = LinkedList() for char4 in 'notintersecting': LL4.push(char4) LL5.push(char4)
#chapter 2 problem 8 from ch2_SetUp import Node, LinkedList # CAUTION: # using printList() *while loop* on a linkedlist with a loop, will cause an infinite loop of printing. LLisLoop = LinkedList() loopNode = Node("LOOP") for i in range(20): if i == 10: LLisLoop.pushNode(loopNode) else: LLisLoop.push(i) #set the loop LLisLoop.end.next = loopNode def findLoop(linkedlist): node1 = linkedlist.begin count = 0 if node1 == None: print("This linked list is empty") return None runner = linkedlist.begin.next if runner: while node1 and runner: if node1 == runner: print("loop node found.")
def sumTwo(ll1, ll2, reverse=True): sum = 0 node1 = ll1.begin node2 = ll2.begin num1 = 0 num2 = 0 tens = 10 count = 0 total = 0 newLL = LinkedList() # What if one linked list is bigger than another? Do we assume they are the same size? if reverse: while node1: current = node1.data if current != 0 and current != '0': num = node1.data * tens**count total = num + total count += 1 node1 = node1.next count = 0 while node2: current = node2.data if current != 0 and current != '0': num = node2.data * tens**count total = num + total count += 1 node2 = node2.next numstring = str(total) for char in numstring: # note: Is this the cheating she refers to? data = int(char) newLL.push(data) else: print('reverse=false') adding = True carry = 0 while adding: data1 = None data2 = None if node1: data1 = node1.data node1 = node1.next if node2: data2 = node2.data node2 = node2.next #print('carry:', carry) #print('data1:', data1) #print('data2:', data2) if data1 == None and data2 == None: break elif data1 != None and data2 != None: total = data1 + data2 + carry elif data1 == None: total = data2 + carry elif data2 == None: total = data1 + carry else: print("ERROR: This should not happen.") break if total >= 10: carry = 1 sumof = total - 10 newLL.push(sumof) else: carry = 0 newLL.push(total) if node1 == None and node2 == None and carry > 0: newLL.push(carry) return newLL
# chapter 2 problem 5 from ch2_SetUp import Node, LinkedList nums = [1, 3, 4] nums2 = [0, 0, 6] nums3 = [1, 0, 8, 9, 9] LL = LinkedList() LL2 = LinkedList() LL3 = LinkedList() for num in nums: LL.push(num) for num2 in nums2: LL2.push(num2) for num3 in nums3: LL3.push(num3) def sumTwo(ll1, ll2, reverse=True): sum = 0 node1 = ll1.begin node2 = ll2.begin num1 = 0 num2 = 0 tens = 10 count = 0 total = 0 newLL = LinkedList() # What if one linked list is bigger than another? Do we assume they are the same size?