educative.io

Why is it saying my solution is wrong when it is correct?

To instructors,

Why is this saying my solution is wrong, when it is outputting the correct reversal for every test case? See my output, it is correctly reversed! yet it is calling it wrong. why?


Course: https://www.educative.io/collection/10370001/5500262945128448
Lesson: https://www.educative.io/collection/page/10370001/5500262945128448/5625390857191424


Course: https://www.educative.io/collection/10370001/5500262945128448
Lesson: https://www.educative.io/collection/page/10370001/5500262945128448/5625390857191424

1 Like

awaiting response


Course: https://www.educative.io/collection/10370001/5500262945128448
Lesson: https://www.educative.io/collection/page/10370001/5500262945128448/5590097835851776

1 Like

Hello @adityahpatel,

Apologies for not responding in time. I have reviewed your solution code and found that you are actually returning a list, not a linked list head. The problem statement specifically asks us to return the head of the linked list which is why the error is occurring “Error in traversing linkedlist”. To fix this issue you can opt the following approach:

    L = []
    temp = head  # Use a temporary variable to iterate through the list
    while temp is not None:
        L.append(temp.data)
        temp = temp.next
    print(L)
    return head # return head instead of L

There is still room for improvement in the provided code but you’ve done a good job in correctly utilising the approach to reverse the linked list. Let me know if there’s anything specific to discuss about. We’d love to hear it from you!

Happy learning!