当前位置:网站首页>【LeetCode】876-链表的中间结点
【LeetCode】876-链表的中间结点
2022-07-02 12:09:00 【酥酥~】
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
示例 1:
输入: [1,2,3,4,5]
输出: 此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
示例 2:
输入: [1,2,3,4,5,6]
输出: 此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
提示:
- 给定链表的结点数介于 1 和 100 之间。
#先得到链表长度,然后再次遍历取中间值
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def middleNode(self, head):
my_iter = head
cnt = 0
while my_iter:
my_iter = my_iter.next
cnt += 1
my_iter = head
for i in range(int(cnt/2)):
my_iter = my_iter.next
return my_iter
#将指针压入数列,然后随机访问取中间值
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def middleNode(self, head):
arr = []
n = 0
while head:
arr.append(head)
head = head.next
n+=1
return arr[int(n/2)]
边栏推荐
- 彻底弄懂浏览器强缓存和协商缓存
- MD5 encryption
- MySQL calculate n-day retention rate
- SQL stored procedure
- JVM architecture, classloader, parental delegation mechanism
- How to find a sense of career direction
- 07_ Hash
- Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
- 12_ Redis_ Bitmap_ command
- Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
猜你喜欢
How to find a sense of career direction
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
06_ Stack and queue conversion
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
YOLOV5 代码复现以及搭载服务器运行
Map介绍
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
N皇后问题的解决
yolo格式数据集处理(xml转txt)
Solution of Queen n problem
随机推荐
Common English abbreviations for data analysis (I)
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
Record an interview
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
16_ Redis_ Redis persistence
yolo格式数据集处理(xml转txt)
数据分析常见的英文缩写(一)
Data analysis thinking analysis methods and business knowledge - business indicators
SQL transaction
工程师评测 | RK3568开发板上手测试
Leetcode skimming - remove duplicate letters 316 medium
. Solution to the problem of Chinese garbled code when net core reads files
4. Jctree related knowledge learning
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
Case introduction and problem analysis of microservice
Set set you don't know
04.进入云原生后的企业级应用构建的一些思考
Leetcode question brushing - parity linked list 328 medium