Definition for singly-linked list.
class ListNode:
def init(self, x):
self.val = x
self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
current = head
pre = None
while current :
temp = current.next
current.next = pre
pre = current
current = temp
return pre
上面是正确的解法
我自己的错误 在于
current.next = pre
pre = current
前后顺序搞反了, 自己指向自己了
网友评论