Insert a node at a specific position in a linked list

  • + 0 comments
    def insertNodeAtPosition(llist, data, position):
        New_node = SinglyLinkedListNode(data)
        if position == 0: # like insert_At_first
            New_node.next = llist
            llist = New_node
        else :
            New_Pos = position
            current = llist
            lenn = 0
            while current.next:
                lenn += 1
                if New_Pos == lenn :
                    break
                current = current.next
            New_node.next = current.next
            current.next = New_node
        return llist