当前位置:网站首页>[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)]
边栏推荐
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- 14_ Redis_ Optimistic lock
- 6.12 critical moment of Unified Process Platform
- 4. Jctree related knowledge learning
- (4) Flink's table API and SQL table schema
- Markdown tutorial
- 11_ Redis_ Hyperloglog_ command
- [network security] network asset collection
- Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
- How to intercept the value of a key from the JSON string returned by wechat?
猜你喜欢

XML Configuration File

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

17_ Redis_ Redis publish subscription

15_Redis_Redis.conf详解

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

04_ Stack

MySQL calculate n-day retention rate

Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July

Leetcode skimming -- sum of two integers 371 medium

Bing.com網站
随机推荐
【LeetCode】695-岛屿的最大面积
Bing.com網站
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
自定义异常
04. Some thoughts on enterprise application construction after entering cloud native
Force deduction solution summary 2029 stone game IX
Beijing rental data analysis
I made an istio workshop. This is the first introduction
Data analysis thinking analysis methods and business knowledge - business indicators
04_ Stack
21_Redis_浅析Redis缓存穿透和雪崩
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
【LeetCode】283-移动零
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
Infra11199 database system
你不知道的Set集合
提前批院校名称
How does the computer set up speakers to play microphone sound
【LeetCode】486-预测赢家
13_ Redis_ affair