当前位置:网站首页>【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)]
边栏推荐
- FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
- Yolov5 code reproduction and server operation
- 二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
- How to conduct TPC-C test on tidb
- 6.12 critical moment of Unified Process Platform
- How to solve the problem of database content output
- 17_Redis_Redis发布订阅
- Recommended configuration of tidb software and hardware environment
- 05_队列
- Kibana basic operation
猜你喜欢

14_Redis_乐观锁

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测

Let your HMI have more advantages. Fet-g2ld-c core board is a good choice

How to choose a third-party software testing organization for automated acceptance testing of mobile applications

18_Redis_Redis主从复制&&集群搭建

让您的HMI更具优势,FET-G2LD-C核心板是个好选择

LeetCode刷题——两整数之和#371#Medium

Bing.com網站

SQL stored procedure

15_Redis_Redis.conf详解
随机推荐
Record an interview
夏季高考文化成绩一分一段表
. Solution to the problem of Chinese garbled code when net core reads files
数据分析思维分析方法和业务知识——业务指标
18_Redis_Redis主从复制&&集群搭建
士官类学校名录
Solution of Queen n problem
彻底弄懂浏览器强缓存和协商缓存
Leetcode skimming -- sum of two integers 371 medium
Pytoch saves tensor to Mat file
LeetCode刷题——去除重复字母#316#Medium
Leetcode skimming - remove duplicate letters 316 medium
02_ Linear table_ Sequence table
How to test tidb with sysbench
10_Redis_geospatial_命令
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
Case introduction and problem analysis of microservice
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
02_线性表_顺序表
19_Redis_宕机后手动配置主机