reverse_linked_list
reverse_linked_list.py 源码
from typing import List
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# 迭代
def reverseList1(self, head: ListNode) -> ListNode:
prev, back = None, head
while back:
back.next, prev, back = prev, back, back.next
return prev
# 递归
def reverseList2(self, head: ListNode) -> ListNode:
def helper(prev, cur) -> ListNode:
if not cur:
return prev
n = cur.next
cur.next = prev
return helper(cur, n)
return helper(None, head)
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦