当前位置:网站首页>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;
}
}
边栏推荐
- AtCoder——Subtree K-th Max
- 2022/7/21训练总结
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 5
- The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap
- phar反序列化
- 重装系统后故障(报错:reboot and select proper boot deviceor insert boot media in selected boot device)
- 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
- 2022DASCTF MAY
- Leetcode 238. 除自身以外数组的乘积
- [深入研究4G/5G/6G专题-40]: URLLC-11-《3GPP URLLC相关协议、规范、技术原理深度解读》-5-5G Qos原理与架构: 切片、PDU会话、QosFlow、5QI、DRB
猜你喜欢

Failure after reinstalling the system (error: Reboot and select proper boot device or insert boot media in selected boot device)

Leetcode 219. 存在重复元素 II(可以,已解决)

phar反序列化

AtCoder——Subtree K-th Max

Win11小组件怎么添加待办事项?Win11添加待办事项小组件的方法

Leetcode 151. 颠倒字符串中的单词

使用多态时,判断能否向下转型的两种思路

17.生命周期

能量原理与变分法笔记16:虚位移原理的求解

Mecol Studio - Little Bear Development Notes 3
随机推荐
Viewing the "Empathy" energy of iqoo 10 pro from 200W super flash charging
ODrive应用 #6 编码器
Top ten shrinking industries in China in the first half of 2022
PostgreSQL sequence cache 参数导致的数值序列不连续 有间隔 gap
Lecture 9 of project practice -- operation import and export tool
ant接口用什么天线_电视后面有个接口写的是 标准ANT 75 欧输入,请问是什么意思,是天线吗?可不可以接闭路线啊?…「建议收藏」
我在代码里面故意留个漏洞,违法吗?
Task03 notes 2
[hero planet July training leetcode problem solving daily] 23rd dictionary tree
Energy principle and variational method note 19: minimum complementary energy principle + possible work principle
2022/7/21训练总结
Adobe Acrobat两个强大的插件
R language uses dwilcox function to generate Wilcoxon rank sum statistical distribution density function data, and uses plot function to visualize Wilcoxon rank sum statistical distribution density fu
JDK installation package and MySQL installation package sorting
Energy principle and variational method note 15: solution of differential element method
从200W超级闪充看iQOO 10 Pro的 “共情”能量
New product listing | A-share floor derivatives market point
梅科尔工作室-小熊派开发笔记2
Odrive application 6 encoder
[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计