当前位置:网站首页>[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)]
边栏推荐
- Wechat Alipay account system and payment interface business process
- How to find a sense of career direction
- How to solve the problem of database content output
- 10_Redis_geospatial_命令
- LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
- 03_線性錶_鏈錶
- MD5加密
- 士官类学校名录
- . Net again! Happy 20th birthday
- FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
猜你喜欢
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
16_Redis_Redis持久化
SQL transaction
03_线性表_链表
Case introduction and problem analysis of microservice
LeetCode刷题——奇偶链表#328#Medium
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
19_ Redis_ Manually configure the host after downtime
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
How to find a sense of career direction
随机推荐
【LeetCode】877-石子游戏
Loss function and positive and negative sample allocation: Yolo series
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
MySQL calculate n-day retention rate
12_ Redis_ Bitmap_ command
Wechat Alipay account system and payment interface business process
NBA player analysis
Common English abbreviations for data analysis (I)
21_Redis_浅析Redis缓存穿透和雪崩
怎样从微信返回的json字符串中截取某个key的值?
18_ Redis_ Redis master-slave replication & cluster building
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
20_ Redis_ Sentinel mode
13_ Redis_ affair
4. Data splitting of Flink real-time project
Bing.com網站
How to find a sense of career direction
05_ queue
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?