当前位置:网站首页>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;
}
}

边栏推荐
- How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?
- Can the training software test be employed
- Redis cache avalanche, penetration, breakdown, bloom filter, detailed explanation of distributed lock
- N32替换STM32,这些细节别忽略!
- 2022 Hangdian multi school field 2 1011 DOS card (line segment tree)
- My creation anniversary -- July 25th, 2022
- QT widget promoted to QWidget
- Mongodb database shell command execution
- The difference between --save Dev and --save in NPM
- unity CS1513
猜你喜欢

Why app uses JSON protocol to interact with server: serialization related knowledge

湖上建仓全解析:如何打造湖仓一体数据平台 | DEEPNOVA技术荟系列公开课第四期

Cause analysis and solution of video jam after easycvr is connected to the device

BM16 删除有序链表中重复的元素-II

数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开

How does the mqtt server built with emqx forward data and save it to the cloud database?

Interpretation of ue4.25 slate source code

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

Use the self-developed proxy server to solve the cross domain access errors encountered when uploading files by SAP ui5 fileuploader trial version

New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held
随机推荐
[machine learning] support vector machine classification
Introduction and advanced level of MySQL (I)
Swiftui swift forward geocoding and reverse geocoding (tutorial with source code)
Is there a future for changing careers in learning software testing?
ECS 5 workflow
4 年后,Debian 终夺回“debian.community”域名!
When unity customizes the editor, let the subclass inherit the inspector display effect of the parent class
使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误试读版
What is one hot code? Why use it and when?
Overview and working principle of single chip microcomputer crystal oscillator
Leetcode skimming - super power 372 medium
unity CS1513
Self cultivation of Electronic Engineers - when a project is developed
BM16 删除有序链表中重复的元素-II
Lookup - lookup of sequential table and ordered table
Can the training software test be employed
SQL custom automatic calculation
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
我的创作纪念日 -- 2022年7月25日
The login interface of modern personal blog system modstartblog v5.4.0 has been revised and the contact information has been added