I have done both Hackerrank and Leetcode reverse LinkedList questions and my code below works perfectly in Leetcode, passing all test cases but fail some tests cases in hacker rank. The same code written in python or C++ passes all the hacker rank test cases. what might be the problem?
The Kotlin code is
if (head == null) return head
var new_head = head
var next_node = new_head.next
while (next_node != null) {
val temp = next_node.next
next_node.next = new_head
new_head = next_node
next_node = temp
}
head.next = null
return new_head
It works perfectly in Leetcode and not in Hacker Rank. The following is the Python version which works perfectly in Hacker Rank. The Python code is the same as Kotlin code.
if head is None:
return None
else:
new_head = head
next_node = new_head.next
while next_node != None:
temp_node = next_node.next
next_node.next = new_head
new_head = next_node
next_node = temp_node
head.next = None
return new_head
The same code written in C++ also works perfectly in Hacker Rank. What might be the problem?