Beispiel #1
0
def partition(a, x):
    headl = taill = Node('x')
    heade = taile = Node('x')
    headg = tailg = Node('x')
    while a:
        nextA = a.next
        if a.data < x:
            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
Beispiel #2
0
def intersection(a, b):
    s = set()
    while a:
        s.add(a.data)
        a = a.next
    head = tail = Node('x')
    while b:
        if b.data in s:
            tail.next = Node(b.data)
            tail = tail.next
        b = b.next
    return head.next
Beispiel #3
0
    def add(self, item):
        current = self.head
        previous = None

        while current is not None:
            if current.value > item:
                break
            previous, current = current, current.next

            temp = Node(item)
            if previous is None:
                temp.next, self.head = self.head, temp
            else:
                temp.next, previous.next = current, temp
def intersection(a, b):
    a = mergeSort(a)
    b = mergeSort(b)
    head = tail = Node('x')
    while a and b:
        if a.data == b.data:
            tail.next = Node(a.data)
            tail = tail.next
            a, b = a.next, b.next
        elif a.data < b.data:
            a = a.next
        else:
            b = b.next
    return head.next
def insertAfterNthFromEnd(l, n, data):
    current = l.head
    for i in range(n):
        if current:
            current = current.next
    if current is None:
        return
    a = l.head
    while current.next:
        a = a.next
        current = current.next
    newNode = Node(data)
    newNode.next = a.next
    a.next = newNode
Beispiel #6
0
def union(a, b):
    s = set()
    head = tail = Node('x')
    while a:
        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
 def setUp(self):
     self.n1 = Node(12)
     self.n2 = Node(12)
     self.n3 = Node(55)
     self.n4 = Node(12)
     self.n5 = Node(12)
     self.n6 = Node(1000)
     self.n7 = Node(12)
     self.n8 = Node(12)
Beispiel #8
0
    def reverseRecursive(self, node: Node) -> Node:

        if node is None or node.next is None:
            return node

        head = self.reverseRecursive(node.next)

        node.next.next = node
        node.next = None

        return head
Beispiel #9
0
def mergeKSortedLists(lists):
    heap = [x for x in lists if x is not None]
    k = len(lists)
    heapq.heapify(heap)
    head = tail = Node('x')
    while len(heap) > 0:
        tail.next = heap[0]
        tail = tail.next
        if heap[0].next:
            heapq.heappushpop(heap, heap[0].next)
        else:
            heapq.heappop(heap)
    return head.next
def union(a, b):
    a = mergeSort(a)
    b = mergeSort(b)
    head = tail = Node('x')
    while a and b:
        if a.data == b.data:
            tail.next = Node(a.data)
            tail = tail.next
            a, b = a.next, b.next
        elif a.data < b.data:
            tail.next = Node(a.data)
            tail = tail.next
            a = a.next
        else:
            tail.next = Node(b.data)
            tail = tail.next
            b = b.next
    if a:
        tail.next = a
    if b:
        tail.next = b
    return head.next
Beispiel #11
0
def sort(a):
    if a is None:
        return a
    first = a
    head = tail = Node('x')
    while a.next:
        tail.next = a.next
        a.next = a.next.next
        tail = tail.next
        tail.next = None
        a = a.next
    second = reverse(head.next)
    return merge(first, second)
Beispiel #12
0
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
Beispiel #13
0
def mergeKSortedLists2(lists):
    heap = [x for x in lists if x is not None]
    k = len(lists)
    buildHeap(heap)
    head = tail = Node('x')
    while len(heap) > 0:
        tail.next = heap[0]
        tail = tail.next
        if heap[0].next:
            heap[0] = heap[0].next
        else:
            heap[0], heap[len(heap) - 1] = heap[len(heap) - 1], heap[0]
            heap.pop()
        heapify(heap, len(heap), 0)
    return head.next
def merge(a, b):
    if a is None:
        return b
    if b is None:
        return a
    head = tail = Node('*')
    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
    return head.next
Beispiel #15
0
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
Beispiel #16
0
from list import LinkedList
from list import Node


def insertIntoMiddle(l, newNode):
    if l.head is None:
        l.head = newNode
        return
    fast, slow, prev = l.head, l.head, None
    while fast and fast.next:
        fast = fast.next.next
        prev, slow = slow, slow.next
    if fast is None:
        newNode.next = slow
        prev.next = newNode
    else:
        newNode.next = slow.next
        slow.next = newNode


a = LinkedList()
a.append(1)
a.append(2)
a.append(3)
a.printList()
insertIntoMiddle(a, Node(10))
a.printList()
from list import Node, List

if __name__ == "__main__":
    names = List()
    names.head = Node("Carol")

    names.push("Bob")
    names.push("Alice")

    names.traverse()

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)