leetcode 反转链表 2021-12-09

2021-12-09  本文已影响0人  远方的飞鱼

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
前后顺序搞反了, 自己指向自己了

上一篇下一篇

猜你喜欢

热点阅读