Exemplo n.º 1
0
def createTestData():
    sll1 = singleLinkedList.LinkedList()
    testData1 = [1, -1, 4, 0, 67, -1, 34]
    # create input linked list
    for _ in testData1[::-1]:
        # O(1)
        sll1.insertStart(_)

    # select intersection point
    intersection = sll1.head
    for _ in range(0, 5):
        intersection = intersection.next

    sll2 = singleLinkedList.LinkedList()
    testData2 = [56, -91, 44, 7, 33]
    # create input linked list
    for _ in testData2[::-1]:
        # O(1)
        sll2.insertStart(_)

    # attach tail of sll2 to intersection point of sll1
    tail = sll2.head
    while tail.next is not None:
        tail = tail.next
    tail.next = intersection
    return sll1, sll2
Exemplo n.º 2
0
def partition(pivot):
    if not sll.isEmpty():
        prev = None
        temp = sll.head
        greaterSLL = singleLinkedList.LinkedList()
        while temp is not None:
            if temp.data >= pivot:
                greaterSLL.insertStart(temp.data)
                sll.deleteNode(prev, temp)
            else:
                prev = temp
            temp = temp.next
        prev.next = greaterSLL.head
Exemplo n.º 3
0
def createTestData():
    sll1 = singleLinkedList.LinkedList()
    testData1 = [1, -1, 4, 0, 67, -1, 34]
    # create input linked list
    for _ in testData1[::-1]:
        # O(1)
        sll1.insertStart(_)

    # corrupt the linked list
    loopPoint = sll1.head
    for _ in range(0, 4):
        loopPoint = loopPoint.next

    tail = sll1.head
    while tail.next is not None:
        tail = tail.next
    tail.next = loopPoint
    return sll1
Exemplo n.º 4
0
 def __init__(self):
     self.items = singleLinkedList.LinkedList()
Exemplo n.º 5
0
import datastructures.linkedLists.src.singleLinkedList as singleLinkedList


def findElementFromLast(k):
    if not sll.isEmpty():
        fast = sll.head
        for _ in range(0, k - 1):
            fast = fast.next
        slow = sll.head
        while fast.next is not None:
            fast = fast.next
            slow = slow.next
        return slow.data


sll = singleLinkedList.LinkedList()
testData = [1, -1, 4, 0, 67, -1, 34, 67, -1, 22, 22, 100, 1, 98, 4, 67]
# create input linked list
for _ in testData[::-1]:
    # O(1)
    sll.insertStart(_)

print(findElementFromLast(int(input("Enter value for k:"))))
Exemplo n.º 6
0
        exp = 0
        while temp is not None:
            sum1 = sum1 + (pow(10, exp) * temp.data)
            exp += 1
            temp = temp.next
    if not sll2.isEmpty():
        temp = sll2.head
        exp = 0
        while temp is not None:
            sum2 = sum2 + (pow(10, exp) * temp.data)
            exp += 1
            temp = temp.next
    return sum1 + sum2


sll1 = singleLinkedList.LinkedList()
sll2 = singleLinkedList.LinkedList()
testData1 = [1, 9]
testData2 = [2, 4, 9]
'''
Output: 
91 + 942 = 1033
19 + 249 = 268
'''
# create input linked list
for _ in testData1[::-1]:
    # O(1)
    sll1.insertStart(_)

for _ in testData2[::-1]:
    # O(1)