链表的中间结点
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
import math
class Solution:
def middleNode(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
# 首先要知道链表一共有多少个结点
count = 0
res = {}
while head != None:
count += 1
res[count] = head
head = head.next
j = math.ceil(count/2)
if count % 2 == 0:
return res[j+1]
else:
return res[j]
# j = math.ceil((count-1)/2) + 1
# return res[j]