def orderdIns(self, item):
        current = self.head
        stop = False
        prev = None

        while current is not None and not stop:
            if current.get_data() > item:
                stop = True
            else:
                prev = current
                current = current.next

        temp = Node(item)

        # check if there is only one node present then it's same as insert at beg
        if prev is None:
            temp.next = self.head
            self.head = temp
            self.length += 1
        # otherwise it's same as inserting the node at specific position
        else:
            prev.next = temp
            temp.next = current
            self.length += 1
Beispiel #2
0
    def mergeTwoSortedLL(self, list1, list2):
        temp = Node(0)

        while list1 is not None and list2 is not None:
            if list1.get_data() < list2.get_data():
                temp.next = list1
                list1 = list1.next
            else:
                temp.next = list2
                list2 = list2.next

        if list1 is None:
            temp.next = list2
        else:
            temp.next = list1

        return temp.next
Beispiel #3
0
            if list1.get_data() < list2.get_data():
                temp.next = list1
                list1 = list1.next
            else:
                temp.next = list2
                list2 = list2.next

        if list1 is None:
            temp.next = list2
        else:
            temp.next = list1

        return temp.next


node1 = Node(1)
node2 = Node(2)
node3 = Node(3)

# create linked list object
ll1 = Linked_List()

# add nodes to linked list
ll1.addNode(node1)
ll1.addNode(node2)
ll1.addNode(node3)

node4 = Node(4)
node5 = Node(5)
node6 = Node(6)
 def add(self, data):
     temp = Node(data)
     temp.next = self.head
     self.head = temp
                print(current.data, end="->")
            else:
                print(current.data)
            current = current.next

    def reverse_recursively(self, item):
        if item.next == None:
            self.head = item
            return
        self.reverse_recursively(item.next)
        temp = item.next
        temp.next = item
        item.next = None


my_list = MyLinkedList()
first_node = Node(1)
second_node = Node(2)
third_node = Node(3)
fourth_node = Node(4)
fifth_node = Node(5)
my_list.add(first_node)
my_list.add(second_node)
my_list.add(third_node)
my_list.add(fourth_node)
my_list.add(fifth_node)

my_list.traverse()
my_list.reverse_recursively(my_list.head)
my_list.traverse()
Beispiel #6
0
def addSameNode(LL1, LL2, value):
    tempNode = Node(value=value)
    LL1.tail.next = tempNode
    LL1.tail = tempNode
    LL2.tail.next = tempNode
    LL2.tail = tempNode