当前位置:网站首页>Force buckle 5_ 876. Intermediate node of linked list
Force buckle 5_ 876. Intermediate node of linked list
2022-07-08 02:15:00 【Don't sleep in class】
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 .
source : Power button (LeetCode)
Java solution 1
A similar is used here C Double pointer method in language , Quick pointer Each move Two grid , Slow pointer Each move One grid Finally, when the fast pointer is finished, the slow pointer is half finished .
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
Java solution 2
class Solution {
public ListNode middleNode(ListNode head) {
ListNode[] A = new ListNode[100];
int t = 0;
while (head != null) {
A[t++] = head;
head = head.next;
}
return A[t / 2];
}
}
Python solution
there Python The traditional method is adopted , First save the linked list into the list , Get the intermediate elements of the list through the index .
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
A = [head]
while A[-1].next:
A.append(A[-1].next)
return A[len(A) // 2]
边栏推荐
- Ml self realization / linear regression / multivariable
- EMQX 5.0 发布:单集群支持 1 亿 MQTT 连接的开源物联网消息服务器
- Redismission source code analysis
- VIM string substitution
- [knowledge map paper] r2d2: knowledge map reasoning based on debate dynamics
- Disk rust -- add a log to the program
- Redisson分布式锁解锁异常
- Ml self realization / logistic regression / binary classification
- node js 保持长连接
- Nacos microservice gateway component +swagger2 interface generation
猜你喜欢
Nmap tool introduction and common commands
Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)
Nanny level tutorial: Azkaban executes jar package (with test samples and results)
MQTT X Newsletter 2022-06 | v1.8.0 发布,新增 MQTT CLI 和 MQTT WebSocket 工具
Towards an endless language learning framework
Random walk reasoning and learning in large-scale knowledge base
Master go game through deep neural network and tree search
《ClickHouse原理解析与应用实践》读书笔记(7)
#797div3 A---C
QT -- create QT program
随机推荐
喜欢测特曼的阿洛
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
Redismission source code analysis
Can you write the software test questions?
Semantic segmentation | learning record (1) semantic segmentation Preface
Semantic segmentation | learning record (3) FCN
Ml self realization / logistic regression / binary classification
生命的高度
电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
云原生应用开发之 gRPC 入门
魚和蝦走的路
Semantic segmentation | learning record (4) expansion convolution (void convolution)
微软 AD 超基础入门
COMSOL --- construction of micro resistance beam model --- final temperature distribution and deformation --- addition of materials
#797div3 A---C
cv2-drawline
VIM string substitution
[reinforcement learning medical] deep reinforcement learning for clinical decision support: a brief overview
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
Disk rust -- add a log to the program