I was doing the reverse a linked list leetcode problem: Reverse a singly linked list. However my code only returns the head although I think the head is linked with its next node by:
pre = curr.next
Below is my code. I'm having difficulty figuring out where the problem is. Any help is appreciated!!
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None:
return head
pre, curr, post = None, head, head.next
while post:
pre = curr.next
pre, curr, post = curr, post, post.next
pre = curr.next
return curr
pre = curr.nextis maybe the wrong way. Trycurr.next = preinstead? - Hitobat