当前位置:网站首页>Parity rearrangement of Bm14 linked list
Parity rearrangement of Bm14 linked list
2022-07-23 20:11:00 【Willow】
subject
The title comes from Niuke
describe
Given a single chain table , Please set a function , Put the odd and even nodes of the linked list together , Output after rearrangement . Note that it is the number of the node, not the value of the node .

Example 1
Input :
{1,2,3,4,5,6}
Return value :
{1,3,5,2,4,6}
explain :
1->2->3->4->5->6->NULL
After rearrangement
1->3->5->2->4->6->NULL
Example 2
Input :
{1,4,6,3,7}
Return value :
{1,6,7,4,3}
explain :
1->4->6->3->7->NULL
After rearrangement
1->6->7->4->3->NULL
Odd nodes have 1,6,7, Even bit nodes have 4,3. After rearrangement 1,6,7,4,3
remarks :
The length of the linked list is not greater than 200000. Each number range is int Inside .
Ideas
According to the title , You can find out odd digit linked list and even digit linked list , Then the last node of the odd linked list next Point to the head node of the even digit linked list , You can string them .
First, you need to save the even number of header nodes , namely resDouble. When traversing the linked list, you can ignore while The execution condition of the loop , Write logic first .
Now let's look at the ergodic condition ,while First used in singleNode and doubleNode Of next, Then make sure that singleNode and doubleNode It can only be taken if it is not empty next, and doubleNode If it is not empty singleNode It must not be empty , because doubleNode yes singleNode Of next, So first add the condition doubleNode != null. And conditions doubleNode.next != null Because if it is empty , Then the odd linked list is shown in the figure above 1 Move to 3 after , There is no need to take it again 3 Of next Assigned to the next node of the even numbered linked list . therefore while On the condition that while(doubleNode != null && doubleNode.next != null).
Code
import java.util.*;
/* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */
public class Solution {
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * * @param head ListNode class * @return ListNode class */
public ListNode oddEvenList (ListNode head) {
// write code here
if(head == null || head.next == null){
return head;
}
ListNode resDouble = head.next;
ListNode singleNode = head;
ListNode doubleNode = head.next;
while(doubleNode != null && doubleNode.next != null){
singleNode.next = doubleNode.next;
singleNode = singleNode.next;
doubleNode.next = singleNode.next;
doubleNode = doubleNode.next;
}
singleNode.next = resDouble;
return head;
}
}
边栏推荐
- Leetcode 228. summary interval (yes, solved)
- Baidu map data visualization
- R language uses the gather function of tidyr package to convert a wide table to a long table (wide table to long table), the first parameter specifies the name of the new data column generated from th
- 能量原理与变分法笔记18:虚力原理
- Ggarrange function of R language ggpubr package combines and annotates multiple images_ Figure add annotation, annotation, annotation information to the combined image, and add annotation information
- Energy principle and variational method note 16: solution of virtual displacement principle
- Lecture 9 of project practice -- operation import and export tool
- 非局部均值滤波(NON-LOCAL-mean)/注意力机制
- Typescript use of new data type symbol
- Win11没有Word文档怎么办?Win11没有Word文档解决教程
猜你喜欢

多子系统多业务模块的复杂数据处理——基于指令集物联网操作系统的项目开发实践

PostgreSQL sequence cache 参数导致的数值序列不连续 有间隔 gap

重装系统后故障(报错:reboot and select proper boot deviceor insert boot media in selected boot device)

千呼万唤,5G双卡双通到底有多重要?

How to solve the problem that the solid state disk cannot be found when installing win11?

搭建自己的目标检测环境,模型配置,数据配置 MMdetection

Energy principle and variational method note 17: generalized variational principle (identification factor method)

MySQL master-slave replication

AtCoder——Subtree K-th Max

编译器LLVM-MLIR-Intrinics-llvm backend-instruction
随机推荐
Leetcode 238. product of arrays other than itself
task03笔记2
Home NAS server (3) | SSD cache acceleration mechanical hard disk
QT with OpenGL (frame cache)
Training log on July 22, 2022
Typescript新数据类型Symbol的使用
MongoDB-查询语句中逻辑运算符not、and、or、nor用法介绍
[PM2] PM2 common commands
编译器LLVM-MLIR-Intrinics-llvm backend-instruction
Leetcode 228. 汇总区间(可以,已解决)
Leetcode 219. duplicate Element II exists (yes, resolved)
The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap
Huawei cloud stack [interview]
Failure after reinstalling the system (error: Reboot and select proper boot device or insert boot media in selected boot device)
2022 the fourth China International elderly care service industry exhibition was held in Jinan on September 26
Leetcode 209. subarray with the smallest length
Energy principle and variational method note 19: minimum complementary energy principle + possible work principle
2022/7/21 training summary
osgearth使用sundog公司的triton海洋和silverlining云彩效果
Top ten shrinking industries in China in the first half of 2022