0

Below is the code for Linked list...we have takeInput() function which takes the input from the user. Also, we have insertAtI() function that is to insert a node anywhere in the linked list. But it's not inserting at the beginning...please have a look...and help me in resolving the issue.

 class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


def takeInput():
    inputList = [int(ele) for ele in input().split()]
    head = None
    tail = None
    for ele in inputList:
        newNode = Node(ele)
        if head is None:
            head = newNode
            tail = newNode
        else:
            tail.next = newNode
            tail = newNode  # or tail = tail.next

    return head


def length(head):
    count = 0
    while head is not None:
        head = head.next
        count += 1
    return count


def insertAtI(head, i, data):

    if i < 0 or i > length(head):
        return head

    count = 0
    prev = None
    curr = head

    while count < i:
        prev = curr
        curr = curr.next
        count += 1

    newNode = Node(data)

    if prev is not None:
        prev.next = newNode
    else:
        head = newNode

    newNode.next = curr

    return head


def printLL(head):
    while head is not None:
        print(head.data, end="->")
        head = head.next
    print("None")


head = takeInput()
printLL(head)


insertAtI(head, 1, 7)
insertAtI(head, 4, 9)
insertAtI(head, 0, 2)
printLL(head)

This code is not inserting the node at the beginning of my linked list. I think the problem is inside insertAtI() function...please help me in resolving the issue.

4
  • 1
    Your insertAtI looks fine to me. You should try adding breakpoint() before you call the function and stepping through your code to debug it. Also, in Python, variables and function names should be named using lowercase_with_underscores not camelCase. Commented May 20, 2021 at 5:10
  • All you need to do is assign the return value of insertAtI to head. Commented May 20, 2021 at 5:27
  • @pppig first two insertions are fine even without assigning, but the third one (i.e. insertAtI(head, 0, 2)) works only when I assign it to some variable. Why is it so ? Commented Jun 6, 2021 at 2:01
  • @Abhinavtyagi the code that prints the linked list starts going over the list from whatever argument you pass in. For example, if your list is 1->2->3->4->None and you do printLL(head.next.next) it'll print 3->4->None. When you insertAtI(head, 0), it creates a new node, but your variable head is still pointing to the old head (which is now the second element in the list), so when you pass it to printLL(head) it's not starting from the new head element you created with insertAtI(). So any time you use insertAtI you need to do head = insertAtI because it returns the correcthead Commented Jun 6, 2021 at 15:48

1 Answer 1

1

You need to overwrite your head variable with whatever head insertAtI returns. It will return a new element as the head if you're inserting into the beginning of the list with insertAtI(head, 0, <whatever>) or the same head you passed in if you're inserting somewhere else in the list. So instead of

insertAtI(head, 1, 7)
insertAtI(head, 4, 9)
insertAtI(head, 0, 2)
printLL(head)

do

head = insertAtI(head, 1, 7)
head = insertAtI(head, 4, 9)
head = insertAtI(head, 0, 2)
printLL(head)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.