当前位置:网站首页>BM14 链表的奇偶重排
BM14 链表的奇偶重排
2022-07-28 17:13:00 【爱生活爱编程a】

思路:本题可以巧妙利用奇偶之间的关联性来解题
public class Solution {
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param head ListNode类 * @return ListNode类 */
public ListNode oddEvenList (ListNode head) {
// write code here
if(head == null){
return null;
}
// 1: 记录第一个奇偶节点
ListNode p1Head = head;
ListNode p2Head = head.next;
ListNode p1 = p1Head;
ListNode p2 = p2Head;
// 2: 奇偶节点利用对方连接属于自己的节点
//注意:结束条件要保证奇节点不为空,因为要靠最后一个奇节点去连接第一个偶节点
//所以根据奇数个节点和偶数个节点两种情况可以确定结束条件
while(p2 != null && p2.next != null){
p1.next = p2.next;
p1 = p1.next;
p2.next = p1.next;
p2 = p2.next;
}
p1.next = p2Head;
return head;
}
}

边栏推荐
- Implementation of grayscale publishing with haproxy
- 行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
- BM16 删除有序链表中重复的元素-II
- What if svchost.exe of win11 system has been downloading?
- Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
- Can I get employed after two months of software testing training?
- Introduction and advanced level of MySQL (II)
- 服务器正文21:不同编译器对预编译的处理(简单介绍msvc和gcc)
- Mongodb initialization
- kotlin:Nothing
猜你喜欢

【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码

Configuration tutorial: how does the organizational structure of the new version of easycvr (v2.5.0) cascade to the superior platform?

我的创作纪念日 -- 2022年7月25日

真正的 HTAP 对用户和开发者意味着什么?
![[actual combat] realize page distortion correction with OpenCV](/img/7b/7e25bde34a9d5463af3dd40599c80e.png)
[actual combat] realize page distortion correction with OpenCV

CTR click through rate prediction practice project of advertising recommendation!

Qt: 一个SIGNAL绑定多个SLOT

What does real HTAP mean to users and developers?

行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开

Is software testing really as good as online?
随机推荐
【数据分析】基于MATLAB实现SVDD决策边界可视化
QT widget promoted to QWidget
Can I get employed after two months of software testing training?
When unity customizes the editor, let the subclass inherit the inspector display effect of the parent class
N32替换STM32,这些细节别忽略!
QT function optimization: QT 3D gallery
What does real HTAP mean to users and developers?
Swiftui swift forward geocoding and reverse geocoding (tutorial with source code)
我的创作纪念日 -- 2022年7月25日
PyG搭建异质图注意力网络HAN实现DBLP节点预测
How to break through the bottleneck of professional development for software testing engineers
Overview and working principle of single chip microcomputer crystal oscillator
Introduction and advanced MySQL (III)
How big is it suitable for learning software testing?
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
Mongodb database shell command execution
Live broadcast platform software development, JS implementation by alphabetical order
注意力机制及代码实现
服务器正文21:不同编译器对预编译的处理(简单介绍msvc和gcc)
What if the content of software testing is too simple?