educative.io

Educative

Simplify Non Palindrome Check at End

while (head is not None and head_second_half is not None):

Only head_second should be checked. This simplifies the code and makes it easier to explain and test! I’ve confirmed this works on the LC problem/tests.


Type your question above this line.

Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/6490214644056064

Hi @Akshay_Goel ,

Thank you so much for reaching out to us. Since we have a code snippet doing head = head.next in the same while loop that is why for safety purposes we have checked head is not None because there could be any situation where it can be crashed.

And I have also applied your suggested changes in the code and it is working perfectly for that given use case but there could be a scenario where we need head is not None.