def addNumbers(l1, l2): lenL1, lenL2 = lenLists(l1, l2) curL1, curL2 = l1, l2 if lenL1 == 0: return l2 if lenL2 == 0: return l1 if lenL1 == lenL2: carry, partSum = addNumbersUtil(l1, l2, Node()) if carry != 0: finalSum = Node(carry) finalSum.next = partSum else: finalSum = partSum return finalSum if lenL1 > lenL2: diffLen = lenL1 - lenL2 for i in range(diffLen): curL1 = curL1.next carry, partSum = addNumbersUtil(curL1, curL2, 0) finalSum = remainingSum(l1, partSum, carry, diffLen) else: diffLen = lenL2 - lenL1 for i in range(diffLen): curL2 = curL2.next carry, partSum = addNumbersUtil(curL1, curL2, 0) finalSum = remainingSum(l2, partSum, carry, diffLen) return finalSum
def sortedMergeIter(l1, l2): head = Node(0) cur = None while l1 or l2: if l1 is None: if cur is not None: cur.next = l2 else: cur = l2 head.next = cur return head.next if l2 is None: if cur is not None: cur.next = l1 else: cur = l1 head.next = cur return head.next if l1.data <= l2.data: if cur is None: cur = l1 head.next = cur else: cur.next = l1 cur = cur.next l1 = l1.next else: if cur is None: cur = l2 head.next = cur else: cur.next = l2 cur = cur.next l2 = l2.next return head.next
def addNumbersUtil(l1, l2, c): if not l1: return None, Node() if not l1.next: val = l1.data + l2.data l = Node(val % 10) return val // 10, l pc, l = addNumbersUtil(l1.next, l2.next, c) newNode = Node() newNode.next = l l = newNode val = l1.data + l2.data + pc l.data = val % 10 return val // 10, l
def intersectionList(l1, l2): l1Set = set() result = Node() head = result while l1: l1Set.add(l1.data) l1 = l1.next while l2: if l2.data in l1Set: result.data = l2.data if l2.next: result.next = Node() result = result.next l2 = l2.next return head
def remainingSum(l1, partSum, carry, diffLen): x = 0 while x < diffLen: newNode = Node() newNode.next = partSum partSum = newNode val = l1.data + carry partSum.data = val % 10 carry = val // 10 x += 1 if carry != 0: newNode = Node() newNode.next = partSum partSum = newNode partSum.data = carry return partSum
def unionList(l1, l2): commonSet = set() result = Node() head = result while l1 and l2: if l1.data not in commonSet: commonSet.add(l1.data) result.data = l1.data if l1.next: result.next = Node() result = result.next if l2.data not in commonSet: commonSet.add(l2.data) result.data = l2.data if l2.next: result.next = Node() result = result.next l1 = l1.next l2 = l2.next return head
def compare(ll1, ll2): while ll1 and ll2 and ll1.data == ll2.data: ll1 = ll1.next ll2 = ll2.next if ll1 and ll2: return 1 if ll1.data > ll2.data else -1 if ll1 and not ll2: return 1 if ll2 and not ll1: return -1 return 0 list1 = Node('g') list1.next = Node('e') list1.next.next = Node('e') list1.next.next.next = Node('k') list1.next.next.next.next = Node('s') list1.next.next.next.next.next = Node('a') list2 = Node('g') list2.next = Node('e') list2.next.next = Node('e') list2.next.next.next = Node('k') list2.next.next.next.next = Node('s') list2.next.next.next.next.next = Node('b') print(compare(list1, list2))
finalSum = partSum return finalSum if lenL1 > lenL2: diffLen = lenL1 - lenL2 for i in range(diffLen): curL1 = curL1.next carry, partSum = addNumbersUtil(curL1, curL2, 0) finalSum = remainingSum(l1, partSum, carry, diffLen) else: diffLen = lenL2 - lenL1 for i in range(diffLen): curL2 = curL2.next carry, partSum = addNumbersUtil(curL1, curL2, 0) finalSum = remainingSum(l2, partSum, carry, diffLen) return finalSum def printList(l): cur = l while cur: print(cur.data, end='->') cur = cur.next print('end') l1 = Node(1) l2 = Node(2) l2.next = Node(3) l = addNumbers(l1, l2) printList(l)