def testCaseForIntersected():
    l1 = LinkedList()

    currentNode = l1.head
    for i in range(5):
        n = Node(i)
        if currentNode is None:
            l1.head = n
            currentNode = l1.head
        else:
            currentNode.next = n
            currentNode = currentNode.next


    l1.printList()

    l2 = LinkedList()

    currentNode = l1.head
    counter = 0
    while counter < 3:
        currentNode = currentNode.next
        counter +=1

    l2.head = currentNode
    l2.printList()

    # call the functions
    result = isIntersectBtw(l1,l2)
    print("Intersect Result: " + str(result))
def testCaseForNotIntersected():
    l1 = LinkedList()
    l1.insertTail(1)
    l1.insertTail(2)
    l1.insertTail(3)

    l2 = LinkedList()
    l2.insertTail(1)
    l2.insertTail(2)
    l2.insertTail(3)

    # call the functions
    result = isIntersectBtw(l1, l2)
    print("Intersect Result: " + str(result))
Beispiel #3
0
def createLinkedListWrt(hashTable):
    newList = LinkedList()

    for key in hashTable.keys():
        newList.insertTail(key)

    return newList
    def _createLinkedList(self, dataList):
        linkedList = LinkedList()

        for i in dataList:
            linkedList.insertTail(i)

        return linkedList
Beispiel #5
0
def getreversedLinkedList(linkedList):
    reversedLinkedList = LinkedList()

    node = linkedList.head
    while node is not None:
        reversedLinkedList.insertHead(node.data)
        node = node.next

    return reversedLinkedList
def convertToLinkedList(number):
    linkedList = LinkedList()

    numberLength = len(str(number))

    division = 1
    for i in range(numberLength - 1):
        division *= 10

    for i in range(numberLength):
        data = int(number / division)
        linkedList.insertTail(data)

        number %= division
        division /= 10

    return linkedList
Beispiel #7
0
def testCaseForLoopDetection():
    #circular linkedlist

    l1 = LinkedList()

    currentNode = l1.head
    for i in range(5):
        n = Node(i)
        if currentNode is None:
            l1.head = n
            currentNode = l1.head
        else:
            currentNode.next = n
            currentNode = currentNode.next

    currentNode.next = l1.head.next.next
    # l1.printList()

    # call the functions
    result = getBeginningNodeForLoop(l1)
    if result:
        print("Started Node for loop: " + str(result.data))
    division = 1
    for i in range(numberLength - 1):
        division *= 10

    for i in range(numberLength):
        data = int(number / division)
        linkedList.insertTail(data)

        number %= division
        division /= 10

    return linkedList


if __name__ == '__main__':
    l1 = LinkedList()
    l1.insertTail(1)
    l1.insertTail(2)
    l1.insertTail(3)

    l1.printList()

    l2 = LinkedList()
    l2.insertTail(3)
    l2.insertTail(2)
    l2.insertTail(1)

    l2.printList()

    l1Number = convertToInteger(l1)
    l2Number = convertToInteger(l2)
Beispiel #9
0
from Utils.LinkedList import LinkedList


def findKthElementFromLast(kth, linkedList):

    if kth > linkedList.size or kth < 1:
        print("Out of scope")
        return None

    index = linkedList.size - kth
    kthNode = linkedList.head
    for i in range(index):
        if kthNode is not None:
            kthNode = kthNode.next

    return kthNode.data


if __name__ == '__main__':
    l1 = LinkedList()
    l1.insertTail(1)
    l1.insertTail(2)
    l1.insertTail(3)
    l1.insertTail(4)

    l1.printList()
    print("findingKthElement")
    result = findKthElementFromLast(5, l1)
    print(result)
Beispiel #10
0
 def __init__(self):
     self.dogList = LinkedList()
     self.catList = LinkedList()
     self.date = 0