middle_of_the_linked_list
middle_of_the_linked_list.py 源码
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
# 使用一个数组存储
def middleNode_1(self, head: ListNode) -> ListNode:
arr = []
while head:
arr.append(head)
head = head.next
return arr[len(arr) // 2]
# 快慢指针法
def middleNode_2(self, head: ListNode) -> ListNode:
slow, fast = head, head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
return slow
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦