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
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)
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: ")
current = current.next def deleteDupsWithoutBuffer(head): current = head while(current != None): runner = current while(runner!=None and runner.next != None): if(runner.next.data == current.data): runner.next = runner.next.next runner = runner.next current = current.next linkedList = LinkedList() linkedList.appendNode(Node(1)) linkedList.appendNode(Node(1)) linkedList.appendNode(Node(2)) linkedList.appendNode(Node(3)) linkedList.appendNode(Node(4)) linkedList.appendNode(Node(4)) linkedList.appendNode(Node(5)) linkedList.appendNode(Node(5)) print("Input: ") linkedList.printList() print("Output: ") # deleteDups(linkedList.head) deleteDupsWithoutBuffer(linkedList.head) linkedList.printList()
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)}")
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)