def sumLists(l1, l2): h1 = l1.getHead() h2 = l2.getHead() result = linkedList() newDigit = 0 while (h1 != None and h2 != None): newDigit += h1.getData() + h2.getData() result.append(newDigit % 10) newDigit //= 10 h1 = h1.getNext() h2 = h2.getNext() while (h1 != None): newDigit += h1.getData() result.append(newDigit % 10) newDigit //= 10 h1 = h1.getNext() while (h2 != None): newDigit += h2.getData() result.append(newDigit % 10) newDigit //= 10 h2 = h2.getNext() return result
def helper(l, current, depth): if current == None: return if depth == len(l): newdepth = linkedList() l[depth] = newdepth l[depth].append(current) helper(l, current.getLeft(), depth + 1) helper(l, current.getRight(), depth + 1)
import sys sys.path.append("/home/ssono/projects/ctci/basics") from structures.linkedList import * """Remove Dups: write code to remove duplicates from an unsorted linkedList""" l = linkedList() l.add('a') l.add('b') l.add('a') l.add('c') l.add('c') l.add('d') l.add('e') print(l) def removeDup(l): seen = {} current = l.getHead() prev = None while (current != None): if current.getData() in seen.keys(): current = current.getNext() prev.setNext(current) else: seen[current.getData()] = 1 prev = current current = current.getNext()
import sys sys.path.append("/home/ssono/projects/ctci/basics") from structures.linkedList import * l1 = linkedList() l1.append(7) l1.append(1) l1.append(6) print(l1) l2 = linkedList() l2.append(5) l2.append(9) l2.append(2) print(l2) def sumLists(l1, l2): h1 = l1.getHead() h2 = l2.getHead() result = linkedList() newDigit = 0 while (h1 != None and h2 != None): newDigit += h1.getData() + h2.getData() result.append(newDigit % 10) newDigit //= 10 h1 = h1.getNext() h2 = h2.getNext() while (h1 != None):