def BackwordSum(ll1, ll2): s1 = StringifyList(ll1) s2 = StringifyList(ll2) s3 = str(int(s1[::-1]) + int(s2[::-1])) ll3 = LinkedList() for i in s3[::-1]: ll3.appendNode(i)
def ForwordSum(ll1, ll2): s1 = StringifyList(ll1) s2 = StringifyList(ll2) s3 = str(int(s1) + int(s2)) ll3 = LinkedList() for i in s3: ll3.appendNode(i)
# Implement an algorithm to delete a node in the middle (i.e., any node but the first and last node, not necessarily the exact middle) of a singly linked list, given only access to that node. # EXAMPLE # lnput:the node c from the linked lista->b->c->d->e->f # Result: nothing is returned, but the new linked list looks likea->b->d->e- >f from linkedList import LinkedList ll = LinkedList() ll.appendNode(1) ll.appendNode(2) ll.appendNode(2) ll.appendNode(1) ll.appendNode(2) ll.appendNode(3) ll.appendNode(3) ll.appendNode(4) ll.appendNode(4) ll.appendNode(5) ll.appendNode(5) def deleteNode(ll, value): if ll.head.value == value: oldHead = ll.head ll.head = ll.head.next oldHead.next = None else: node = ll.head while node and node.next: if node.next.value == value: nextNode = node.next node.next = nextNode.next
# Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list, the values of x only need to be after the elements less than x (see below). The partition element x can appear anywhere in the "right partition"; it does not need to appear between the left and right partitions. # EXAMPLE # Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [partition=5] # Output: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8 from linkedList import LinkedList ll = LinkedList() ll.appendNode(11) ll.appendNode(3) ll.appendNode(8) ll.appendNode(5) ll.appendNode(10) ll.appendNode(2) ll.appendNode(12) def Partition(ll, value): node = ll.head.next prev = ll.head dict = {} while node: if node.value < value: prev.next = node.next old_head = ll.head ll.head = node node.next = old_head node = prev.next else: prev = node node = node.next
# Given two (singly) linked lists, determine if the two lists intersect. Return the inter secting node. Note that the intersection is defined based on reference, not value.That is, if the kth node of the first linked list is the exact same node (by reference) as the jth node of the second linked list, then they are intersecting. from linkedList import LinkedList ll1 = LinkedList() ll1.appendNode(11) ll1.appendNode(3) ll1.appendNode(8) ll1.appendNode(5) ll1.appendNode(10) ll1.appendNode(2) ll1.appendNode(12) ll2 = LinkedList() ll2.appendNode(11) ll2.appendNode(3) ll2.appendNode(8) ll2.appendNode(5) ll2.appendNode(10) ll2.appendNode(2) ll2.appendNode(12) def CheckIntersection(ll1, ll2): node1 = ll1.head node2 = ll2.head dict = {} while node1: dict[node1.value] = node1.next node1 = node1.next while node2: