1、解题思路
2、代码
【图源Leetcode 206 题解 侵删】
# 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:
pre = None
cur = head
while cur is not None:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
Python能一句话说完的绝不两句【人生苦短,我用Python】
# 人生苦短,我用Python
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre, cur = None, head
while cur is not None:
cur.next, pre, cur = pre, cur, cur.next
return pre