当前位置:网站首页>LeetCode_双指针_中等_328.奇偶链表
LeetCode_双指针_中等_328.奇偶链表
2022-06-29 11:50:00 【一瓢江湖我沉浮】
1.题目
给定单链表的头节点 head ,将所有索引为奇数的节点和索引为偶数的节点分别组合在一起,然后返回重新排序的列表。
第一个节点的索引被认为是奇数, 第二个节点的索引为偶数,以此类推。
请注意,偶数组和奇数组内部的相对顺序应该与输入时保持一致。
你必须在 O(1) 的额外空间复杂度和 O(n) 的时间复杂度下解决这个问题。
示例 1:
输入: head = [1,2,3,4,5]
输出: [1,3,5,2,4]
示例 2:
输入: head = [2,1,3,5,6,4,7]
输出: [2,3,6,7,1,5,4]
提示:
n == 链表中的节点数
0 <= n <= 104
-106 <= Node.val <= 106
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/odd-even-linked-list
2.思路
(1)双指针
具体细节可参考本题官方题解。
3.代码实现(Java)
//思路1————双指针
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null) {
return head;
}
ListNode odd = head;
ListNode evenHead = head.next;
ListNode even = evenHead;
while (even != null && even.next != null) {
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
//将偶数链表连接在奇数链表之后
odd.next = evenHead;
return head;
}
}
边栏推荐
- [comprehensive case] credit card virtual transaction identification
- 求大数的阶乘 ← C语言
- How to install oracle19c in Centos8
- Interpolated scatter data
- 东方财富证券开户安全吗 证券开户办理
- Gbase8s database select has order by Clause 2
- Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
- Syntax of gbase8s database incompatible with for update clause
- Earth observation satellite data
- After class assignment of module 5 of the construction practice camp
猜你喜欢

《高难度谈话》突破谈话瓶颈,实现完美沟通

面试突击61:说一下MySQL事务隔离级别?

go 学习-搭建开发环境vscode开发环境golang

【综合案例】信用卡虚拟交易识别

An interpretable geometric depth learning model for structure based protein binding site prediction

Titanium dynamic technology: our Zadig landing Road

Cocos star meetings at Hangzhou station in 2022

ERP编制物料清单 金蝶

Unified exception reporting practice based on bytecode

Paper reproduction - ac-fpn:attention-guided context feature pyramid network for object detection
随机推荐
力扣每日一题-第31天-1779.找到最近的有相同x或y坐标的点
Interview shock 61: tell me about MySQL transaction isolation level?
牛顿不等式
Understanding of P value
Gbase8s database select has order by Clause 6
MySQL 主从复制原理以及流程
墨菲安全入选中关村科学城24个重点项目签约
Principle and process of MySQL master-slave replication
一种可解释的几何深度学习模型,用于基于结构的蛋白质结合位点预测
Interpolated scatter data
GBase8s数据库INTO TEMP 子句创建临时表来保存查询结果。
速看|期待已久的2022年广州助理检测工程师真题解析终于出炉
Gbase8s database into standard and into raw clauses
Helping the ultimate experience, best practice of volcano engine edge computing
Cocos star meetings at Hangzhou station in 2022
How to create new user for ORACLE 19c (CDB & PDB)
解决问题:ModuleNotFoundError: No module named ‘pip‘
《自卑与超越》生活对你应有的意义
oracle 19c : change the user sys/system username pasword under Linux
Wonderful! Miaoying technology fully implements Zadig to help container construction, and fully embraces kubernetes and Yunyuan