赞
踩
示例
我使用的是笨方法,用一个数组(列表)依次存储节点的值,将列表中的值反转,再修改链表的数值。
#Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
代码:
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head == None:
return head
h = head
a = []
while h != None:
a.append(h.val)
h = h.next
a = a[::-1]
d = head
for i in range(len(a)):
d.val = a[i]
d = d.next
return head
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。