def intersectionOfSortedLists(a, b): result = LinkedList() while a and b: if a.data == b.data: result.append(a.data) a, b = a.next, b.next elif a.data > b.data: b = b.next else: a = a.next return result
def isPalindrome(node): s = [] current = node while current: s.append(current.data) current = current.next current = node while current: if current.data != s.pop(): return False current = current.next return True a = LinkedList() a.printList() print isPalindrome(a.head) a.append(1) a.printList() print isPalindrome(a.head) a.append(2) a.append(1) a.printList() print isPalindrome(a.head) a.append(2) a.append(1) a.printList() print isPalindrome(a.head) a.append(5) a.printList() print isPalindrome(a.head)
prevI, prevJ = None, None while j and k > 1: prevJ, j = j, j.next k -= 1 if j is None: return newHead kthFromBegin, kthFromEnd = j, i while j.next: prevI, kthFromEnd = kthFromEnd, kthFromEnd.next j = j.next if kthFromBegin == kthFromEnd: return newHead if prevI is None: newHead = kthFromBegin prevJ.next = kthFromEnd elif prevJ is None: newHead = kthFromEnd prevI.next = kthFromBegin else: prevI.next = kthFromBegin prevJ.next = kthFromEnd kthFromBegin.next, kthFromEnd.next = kthFromEnd.next, kthFromBegin.next return newHead a = LinkedList() for i in range(1, 9): a.append(i) a.printList() a.head = swap(a.head, 1) a.printList()
if (a.data > b.data): a.data, b.data = b.data, a.data temp = b while b.next and b.data > b.next.data: b = b.next prev, ptr = None, b while ptr and ptr.data < temp.data: prev, ptr = ptr, ptr.next prev.next = temp temp.next = ptr if a.next is None: break a = a.next while a.next: a = a.next a.next = b a = LinkedList() a.append(2) a.append(4) a.append(7) a.append(8) a.append(10) b = LinkedList() b.append(1) b.append(3) b.append(12) merge(a.head, b.head) a.printList()
def reverse(node, k, i): current, next, prev, count = node, None, None, 0 if i == 0: while current and count < k: next = current.next current.next = prev prev = current current = next count += 1 else: while current and count < k: prev, current, next = current, current.next, current.next count += 1 if next: if i == 0: node.next = reverse(next, k, 1) else: prev.next = reverse(next, k, 0) return prev if i == 0 else node l = LinkedList() for i in range(1, 9): l.append(i) l.printList() l.head = reverse(l.head, 3, 0) l.printList()
from list import Node from list import LinkedList def isLoop(node): fast, slow = node, node while fast and fast.next: fast = fast.next.next slow = slow.next if fast == slow: return True return False a = LinkedList() a.push(1) a.append(2) a.append(3) a.append(4) a.append(5) a.printList() print isLoop(a.head) a.head.next.next.next.next.next = a.head.next.next.next print isLoop(a.head)
def deleteMiddlePoints(node): while node and node.next and node.next.next: if node.data[0] == node.next.data[0] and node.data[ 0] == node.next.next.data[0]: node.next = node.next.next continue if node.data[1] == node.next.data[1] and node.data[ 1] == node.next.next.data[1]: node.next = node.next.next continue node = node.next a = LinkedList() a.append([0, 10]) a.append([1, 10]) a.append([5, 10]) a.append([7, 10]) a.append([7, 5]) a.append([20, 5]) a.append([40, 5]) a.printList() deleteMiddlePoints(a.head) a.printList() b = LinkedList() b.append([2, 3]) b.append([5, 3]) b.append([7, 3]) b.append([10, 3])
else: result.head = Node(sum % 10) lastNode = result.head carry = sum / 10 a = a.next while b: sum = b.data + carry if lastNode: lastNode.next = Node(sum % 10) lastNode = lastNode.next else: result.head = Node(sum % 10) lastNode = result.head carry = sum / 10 b = b.next if carry == 1: if lastNode: lastNode.next = Node(1) else: result.head = Node(1) return result a = LinkedList() a.append(6) a.append(7) a.append(5) b = LinkedList() add(a.head, b.head).printList()
from list import LinkedList from list import Node def isPalindrome(node): s = "" while node: s += node.data node = node.next l = 0 r = len(s) - 1 while l < r: if s[l] != s[r]: return False l += 1 r -= 1 return True a = LinkedList() a.append("A") a.append("BC") a.append("D") a.append("DCB") a.append("A") a.printList() print isPalindrome(a.head)
even = [] odd = [] i = 0 current = l.head while current: if i % 2 == 0 and current.data % 2 == 1: odd.append(current) if i % 2 == 1 and current.data % 2 == 0: even.append(current) i += 1 current = current.next while len(odd) > 0 and len(even) > 0: a, b = odd.pop(), even.pop() a.data, b.data = b.data, a.data l = LinkedList() l.append(10) l.append(2) l.append(1) l.append(5) l.append(3) l.append(6) l.append(7) l.append(8) l.append(9) l.append(10) l.printList() alternateOddEvenEfficient(l) l.printList()
tail.next = b b = b.next tail = tail.next if a: tail.next = a if b: tail.next = b return head def findMid(a): prevSlow, slow, fast = None, a, a while fast and fast.next: fast = fast.next.next prevSlow, slow = slow, slow.next return prevSlow a = [4, 3, 5, 7, 11, 2, 1] l1 = LinkedList() for i in a: l1.append(i) l1.printList() a = [2, 3, 4, 5, 6, 8, 12] l2 = LinkedList() for i in a: l2.append(i) l2.printList() print countPairsWithSumX(l1.head, l2.head, 9)
from list import Node from list import LinkedList def frequency(node, k): count = 0 while node: if node.data == k: count += 1 node = node.next return count a = LinkedList() a.append(0) a.push(1) a.append(1) a.push(2) a.append(3) a.push(2) a.append(4) a.push(2) a.append(2) a.push(0) a.printList() for i in range(0, 6): print frequency(a.head, i)
prev.next = current.next current = current.next else: maxRight = current.data prev = current current = current.next reverse(l) def reverse(l): current, prev = l.head, None while current: next = current.next current.next = prev prev, current = current, next l.head = prev l = LinkedList() l.append(12) l.append(15) l.append(10) l.append(11) l.append(5) l.append(6) l.append(2) l.append(3) l.printList() delete(l) l.printList()
if current.data == key: prevLastOcc, lastOcc = prev, current prev, current = current, current.next if lastOcc is None: return if prevLastOcc is None: l.head = l.head.next else: prevLastOcc.next = lastOcc.next lastOcc.next = None lastOcc = None l = LinkedList() for i in range(8): l.append(i) l.printList() deleteLast(l, 6) l.printList() deleteLast(l, 7) l.printList() deleteLast(l, 7) l.printList() deleteLast(l, 1) l.printList() deleteLast(l, 2) deleteLast(l, 3) deleteLast(l, 4) deleteLast(l, 0) deleteLast(l, 5) l.printList()
from list import Node def mergeRecursive(a, b): if a is None: return b if b is None: return a if a.data <= b.data: a.next = mergeRecursive(a.next, b) return a else: b.next = mergeRecursive(a, b.next) return b l = LinkedList() l.append(1) l.append(2) l.append(4) l.printList() s = LinkedList() s.append(3) s.append(4) s.append(5) s.printList() m = LinkedList() m.head = mergeRecursive(l.head, s.head) m.printList()
if a.data not in s: s.add(a.data) tail.next = Node(a.data) tail = tail.next a = a.next while b: if b.data not in s: s.add(b.data) tail.next = Node(b.data) tail = tail.next b = b.next return head.next a = LinkedList() a.append(10) a.append(15) a.append(4) a.append(20) a.printList() b = LinkedList() b.append(8) b.append(4) b.append(2) b.append(10) b.printList() i = LinkedList() i.head = intersection(a.head, b.head) i.printList()
lastNode.next = current lastNode = lastNode.next current.next = None current = l.head else: prev.next = current.next lastNode.next = current lastNode = lastNode.next current.next = None current = prev.next else: prev, current = current, current.next a = LinkedList() a.append(1) a.append(1) a.append(0) a.append(2) a.append(0) a.append(0) a.append(1) a.append(2) a.append(0) a.append(1) a.append(2) a.append(2) a.append(1) a.append(1) a.append(2) a.printList()
else: if not firstNonNegNode: return reverse(firstNegNode) else: prev.next = None s = reverse(firstNegNode) firstNegNode.next = firstNonNegNode return s def reverse(a): prev, current = None, a while current: next = current.next current.next = prev prev, current = current, next return prev a = LinkedList() a.append(0) a.append(-2) a.append(-3) a.append(4) a.append(-5) a.append(-6) a.append(-8) a.printList() a.head = sort(a.head) a.printList()
def strCompare(a, b): if not a and not b: return 0 if not a: return -1 if not b: return 1 if a.data != b.data: return sgn(ord(a.data), ord(b.data)) return strCompare(a.next, b.next) def sgn(a, b): if a > b: return 1 elif b > a: return -1 return 0 a = LinkedList() b = LinkedList() s1 = "Archit" s2 = "Arpit" for i in s1: a.append(i) for i in s2: b.append(i) print strCompare(a.head, b.head)
def heapify(a, n, i): smallest = i l = 2 * i + 1 r = l + 1 if l < n and a[l] < a[smallest]: smallest = l if r < n and a[r] < a[smallest]: smallest = r if i != smallest: a[smallest], a[i] = a[i], a[smallest] heapify(a, n, smallest) a = LinkedList() a.append(1) a.append(4) a.append(5) b = LinkedList() b.append(1) b.append(3) b.append(4) c = LinkedList() c.append(2) c.append(6) a.printList() b.printList() c.printList() mergedList = LinkedList() mergedList.head = mergeKSortedLists2([a.head, b.head, c.head]) mergedList.printList()
from list import Node from list import LinkedList def intersectionOfSortedLists(a, b): result = LinkedList() while a and b: if a.data == b.data: result.append(a.data) a, b = a.next, b.next elif a.data > b.data: b = b.next else: a = a.next return result a = LinkedList() for i in range(8): a.append(i) a.printList() b = LinkedList() for i in range(1, 6): b.append(i*2) b.printList() intersectionOfSortedLists(a.head, b.head).printList()
from list import LinkedList from list import Node def isIdentical(a, b): if not a and not b: return True if not a or not b: return False return a.data == b.data and isIdentical(a.next, b.next) a = LinkedList() a.append(1) b = LinkedList() b.append(1) b.append(2) print isIdentical(a.head, b.head)
from list import Node from list import LinkedList def multiply(a, b): n1 = n2 = 0 while a: n1 = n1 * 10 + a.data a = a.next while b: n2 = n2 * 10 + b.data b = b.next return n1 * n2 a = LinkedList() a.append(9) a.append(4) a.append(6) a.printList() b = LinkedList() b.append(8) b.append(4) b.printList() print multiply(a.head, b.head)
from list import LinkedList from list import Node def makeMiddleHead(l): fast, slow, prev = l.head, l.head, None while fast and fast.next: fast = fast.next.next prev, slow = slow, slow.next if prev: prev.next = slow.next slow.next = l.head l.head = slow a = LinkedList() a.append(1) a.append(2) a.append(3) a.append(4) a.printList() makeMiddleHead(a) a.printList()
from list import Node def subListSearch(a, b): if b is None: return True while a: currentA, currentB = a, b while currentA and currentB: if currentA.data != currentB.data: break currentA, currentB = currentA.next, currentB.next if not currentB: return True if currentA is None: return False a = a.next return False a = LinkedList() for i in range(1, 9): a.append(i) a.printList() b = LinkedList() b.append(4) b.printList() print subListSearch(a.head, b.head)
prev, node = node, node.next firstMovedNode = None while node and node != firstMovedNode: if (node.data % 2 == 0 and isHeadEven) or (node.data % 2 != 0 and not isHeadEven): prev, node = node, node.next else: if not firstMovedNode: firstMovedNode = node prev.next = node.next lastNode.next = node node.next = None lastNode = lastNode.next node = prev.next a = LinkedList() a.append(17) a.append(15) a.append(8) a.append(12) a.append(10) a.append(5) a.append(4) a.append(1) a.append(7) a.append(6) a.printList() segregateEvenAndOdd(a.head) a.printList()
current = node while current: a[current.data] += 1 current = current.next current = node i = 0 while current and i < 3: current.data = i current = current.next a[i] -= 1 if a[i] == 0: i += 1 l = LinkedList() l.append(1) l.append(0) l.append(2) l.append(2) l.append(1) l.append(1) l.append(1) l.append(0) l.append(0) l.append(0) l.append(2) l.append(1) l.append(1) l.append(2) l.append(1) l.append(0)
s = [] while d > 0: s.append([p.data, 0]) p = p.next d -= 1 while p and q: s.append([(p.data + q.data) % 10, (p.data + q.data) / 10]) p = p.next q = q.next l = LinkedList() carry = 0 while len(s) > 0: node = s.pop() l.push((node[0] + carry) % 10) carry = node[1] + (node[0] + carry) / 10 if carry > 0: l.push(carry) return l n1 = LinkedList() s1 = "1999" s2 = "1001" for i in s1: n1.append(int(i)) n1.printList() n2 = LinkedList() for i in s2: n2.append(int(i)) n2.printList() add(n1, n2).printList()
if l1 > l2: d = l1 - l2 while d > 0: p = p.next d -= 1 elif l2 < l1: d = l1 - l2 while d > 0: q = q.next d -= 1 while p != q: p, q = p.next, q.next return p.data a = LinkedList() b = LinkedList() for i in range(8): a.append(i) for j in range(8, 10): b.append(j) b.head.next.next = a.head.next.next.next.next.next a.printList() b.printList() print intersectionPoint(a, b)
b = b.next mergedTail = mergedHead while a and b: if a.data <= b.data: mergedTail.next = a a = a.next else: mergedTail.next = b b = b.next mergedTail = mergedTail.next if a: mergedTail.next = a if b: mergedTail.next = b return mergedHead a = LinkedList() a.append(1) a.append(2) a.append(3) b = LinkedList() b.append(4) b.append(5) b.append(6) a.printList() b.printList() l = LinkedList() l.head = merge(a.head, b.head) a.printList()