def alternateSegregate(l): if l.head is None: return first, second = LinkedList(), LinkedList() lastNode = l.head while lastNode.next: lastNode = lastNode.next current = l.head lNode = lastNode prev = None while current != lastNode: next = current.next current.next = next.next lNode.next = next next.next = None lNode = lNode.next prev = current current = current.next if lNode == lastNode: break first.head = l.head second.head = current if lastNode == lNode else current.next if lastNode != lNode: current.next = None else: print current.data prev.next = None return [first, second]
def mergeKSortedLists(lists): while len(lists) > 1: p, q = lists.pop(0), lists.pop(0) lists.append(merge(p, q)) l = LinkedList() l.head = lists[0] return l
def add(a, b): result = LinkedList() lastNode = None carry = 0 while a and b: sum = a.data + 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 a, b = a.next, b.next while a: sum = a.data + carry if lastNode: lastNode.next = Node(sum % 10) lastNode = lastNode.next 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
def merge(a, b): l = LinkedList() while a and b: if a.data <= b.data: temp = a.next a.next = l.head l.head = a a = temp else: temp = b.next b.next = l.head l.head = b b = temp while a: temp = a.next a.next = l.head l.head = a a = temp while b: temp = b.next b.next = l.head l.head = b b = temp return l
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()
tail = head while a and b: if a.data <= b.data: tail.next = a a = a.next else: tail.next = b b = b.next tail = tail.next if a: tail.next = a if b: tail.next = b return head def reverse(node): prev, current = None, node while current: next = current.next current.next = prev prev, current = current, next return prev s = [10, 40, 53, 30, 67, 12, 89] a = LinkedList() for i in s: a.append(i) a.printList() a.head = sort(a.head) a.printList()
taill.next = a taill = taill.next elif a.data == x: taile.next = a taile = taile.next else: tailg.next = a tailg = tailg.next a.next = None a = nextA headl = headl.next headg = headg.next heade = heade.next if headl: if heade: taill.next = heade else: taill.next = headg if heade: taile.next = headg head = headl if headl else heade if heade else headg return head a = LinkedList() s = [1, 4, 3, 2, 5, 2, 3] for i in s: a.append(i) a.printList() a.head = partition(a.head, 3) a.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 b is None: return a if a.data <= b.data: mergedHead = a a = a.next else: mergedHead = 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 = [4, 3, 1, 2, 8, 7, 6, 1, 4, 9, 3, 4, 5, 2, 1] l = LinkedList() for i in a: l.append(i) l.head = mergeSort(l.head) l.printList()
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()
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()
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() u = LinkedList() u.head = union(a.head, b.head) u.printList() a.append(10) a.printList() b.printList() u.head = union(a.head, b.head) u.printList()
return first def leftRotate(node, k): if node is None or node.next is None: return node size = 1 lastNode = node while lastNode.next: lastNode = lastNode.next size += 1 k = k % size if k == 0: return node lastNode.next = node q = node while k > 1: q = q.next k -= 1 node = q.next q.next = None return [node, q] a = LinkedList() for i in range(8): a.append(i) a.printList() a.head = rotateBlockWiseUtil(a.head, 4, 2) a.printList()
tail.next = a a = a.next else: tail.next = b b = b.next tail = tail.next if a: tail.next = a if b: tail.next = b return head 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() a.printList() #intersect = LinkedList() #intersect.head = intersection(a.head, b.head) #intersect.printList() un = LinkedList() un.head = union(a.head, b.head) un.printList()
def printList(a): fast, slow = a, a loopNode = None while fast and fast.next: print slow.data, fast, slow = fast.next.next, slow.next if fast == slow: loopNode = slow break if not loopNode: while slow: print slow.data, slow = slow.next else: while a != loopNode: print loopNode.data, a, loopNode = a.next, loopNode.next a = LinkedList() a.head = Node(1) a.head.next = Node(2) a.head.next.next = Node(3) a.head.next.next.next = Node(2) a.head.next.next.next.next = Node(1) a.head.next.next.next.next.next = a.head.next.next printList(a.head) print checkIfPalindrome(a.head) printList(a.head)
def reverse(a): prev, current = None, a while current: next = current.next current.next = prev prev, current = current, next return prev def mergeAlternate(a, b): head = tail = Node('x') while a and b: tail.next = a tail = tail.next a = a.next tail.next = b tail = tail.next b = b.next if a: tail.next = a if b: tail.next = b return head.next a = LinkedList() s = [10, 4, 7, 1, 3, 8, 9, 2, 0, 5, 6] for i in s: a.append(i) a.printList() a.head = rearrange(a.head) a.printList()
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()
from list import LinkedList from list import Node def swap(a): if a is None or a.next is None: return a temp = swap(a.next.next) nextA = a.next a.next = temp nextA.next = a return nextA a = LinkedList() for i in range(9): a.append(i) a.printList() a.head = swap(a.head) a.printList()