当前位置:网站首页>[leetcode] 876 intermediate node of linked list
[leetcode] 876 intermediate node of linked list
2022-07-02 15:36:00 【Crisp ~】
Given a header node as head The non empty single chain table of , Returns the middle node of the linked list .
If there are two intermediate nodes , Then return to the second intermediate node .
Example 1:
Input : [1,2,3,4,5]
Output : Nodes in this list 3 ( Serialization form :[3,4,5])
The returned node value is 3 . ( The evaluation system expresses the serialization of this node as follows [3,4,5]).
Be careful , We returned one ListNode Object of type ans, such :
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, as well as ans.next.next.next = NULL.
Example 2:
Input : [1,2,3,4,5,6]
Output : Nodes in this list 4 ( Serialization form :[4,5,6])
Because the list has two intermediate nodes , Values, respectively 3 and 4, Let's go back to the second node .
Tips :
- The number of nodes in a given list is between 1 and 100 Between .
# Get the length of the linked list first , Then traverse again to get the intermediate value
# 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
# Press the pointer into the sequence , Then random access takes the intermediate value
# 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)]
边栏推荐
- 04_ Stack
- LeetCode刷题——验证二叉树的前序序列化#331#Medium
- 夏季高考文化成绩一分一段表
- Practical debugging skills
- SQL stored procedure
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
- 2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
- Common English abbreviations for data analysis (I)
- How does the computer set up speakers to play microphone sound
- PTA 天梯赛习题集 L2-001 城市间紧急救援
猜你喜欢

【LeetCode】1162-地图分析

03_线性表_链表

07_ Hash

自定义异常

yolo格式数据集处理(xml转txt)

05_ queue

JVM architecture, classloader, parental delegation mechanism

There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant

Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!

Leetcode skimming - remove duplicate letters 316 medium
随机推荐
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
Bing. Site Internet
12_Redis_Bitmap_命令
Redux - detailed explanation
. Net again! Happy 20th birthday
20_ Redis_ Sentinel mode
yolo格式数据集处理(xml转txt)
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
【LeetCode】695-岛屿的最大面积
[network security] network asset collection
【LeetCode】1140-石子游戏II
LeetCode刷题——验证二叉树的前序序列化#331#Medium
损失函数与正负样本分配:YOLO系列
13_ Redis_ affair
Pytorch 保存tensor到.mat文件
02.面向容器化后,必须面对golang
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
Map introduction
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香