当前位置:网站首页>leetCode-链表的中间结点
leetCode-链表的中间结点
2022-06-26 09:36:00 【later_rql】
LeetCode-链表的中间结点
题目描述:给定一个头结点为 head 的非空单链表,返回链表的中间结点
如果有两个中间结点,则返回第二个中间结点。
思路:
- 方法一:java快慢指针
class Solution {
public ListNode middleNode(ListNode head) {
ListNode p = head, q = head;
while (q != null && q.next != null) {
q = q.next.next;
p = p.next;
}
return p;
}
}
- 方法二:单指针法
class Solution {
public ListNode middleNode(ListNode head) {
int n = 0;
ListNode cur = head;
while (cur != null) {
++n;
cur = cur.next;
}
int k = 0;
cur = head;
while (k < n / 2) {
++k;
cur = cur.next;
}
return cur;
}
}
边栏推荐
- Openxcap usage
- This new change of go 1.16 needs to be adapted: the changes of go get and go install
- MapReduce & yarn theory
- npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.npm ER
- LeetCode 498. 对角线遍历
- P1296 whispers of cows (quick row + binary search)
- Constraintlayout control uses full Raiders
- 微软 Edge 浏览器 IE 模式标签页出现卡死情况,已通过回滚更新修复
- 逻辑英语结构【重点】
- 力扣------从数组中移除最大值和最小值
猜你喜欢

A list of common methods for customizing paint and canvas of view

Day 3 array, pre post, character space, keyword and address pointer

The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions

微软 Edge 浏览器 IE 模式标签页出现卡死情况,已通过回滚更新修复

Mysql database field query case sensitive setting

MapReduce & yarn theory

The basis of C language grammar -- pointer (multidimensional array, function, summary) learning

The 100000 line transaction lock has opened your eyes.

Configuration internationale

Notes on sports planning on November 22, 2021
随机推荐
The 100000 line transaction lock has opened your eyes.
Abnormal record-23
P1296 whispers of cows (quick row + binary search)
Day 3 array, pre post, character space, keyword and address pointer
How to find and install the dependent libraries of Debian system
druid数据源实现后台监控
mysql学习总结
Introduction to stored procedure testing
Various errors encountered by tensorflow
我的创作纪念日
Redis master-slave replication in win10 system
The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions
libmagic 介绍
logback
Testing practice - App testing considerations
install opencv-contrib-dev to use aruco code
Download MySQL database installation package website of each system and version
c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
LeetCode 498. Diagonal traversal
How about the security of flush stock trading software? How to open an account in flush