当前位置:网站首页>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;
}
}
边栏推荐
- 2022/7/21训练总结
- Leetcode - the nearest sum of three numbers
- 能量原理与变分法笔记16:虚位移原理的求解
- 2022DASCTF MAY
- Attack and defense world web question Fakebook
- Cannot read properties of null (reading ‘pickAlgorithm‘)
- Mecol Studio - Little Bear Development Notes 3
- Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC
- 干货!神经网络中的隐性稀疏正则效应
- Win11小组件怎么添加待办事项?Win11添加待办事项小组件的方法
猜你喜欢

21. Mix in details

Model loading of assimp Library under QT

The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap

能量原理与变分法笔记15:微元法的求解
![[development experience] development project trample pit collection [continuous update]](/img/02/7bea3bf09e9a27b6ab74399639f197.png)
[development experience] development project trample pit collection [continuous update]

Leetcode 152. product maximum subarray (brute force cracking can actually pass!)

MongoDB-查询语句中逻辑运算符not、and、or、nor用法介绍

Mekol Studio - Little Bear Development Notes 2

2022DASCTF MAY

Osgearth uses sundog's Triton ocean and silverlining cloud effects
随机推荐
Typescript新数据类型Symbol的使用
21.mixin混入详解
能量原理与变分法笔记14:总结+问题的求解
What if there is no word document in win11? There is no word document solution tutorial in win11
phar反序列化
How to reset the computer system? The method is actually very simple
17. Life cycle
从200W超级闪充看iQOO 10 Pro的 “共情”能量
Mekol Studio - Little Bear Development Notes 2
Leetcode 209. subarray with the smallest length
Task03 | return
Cannot read properties of null (reading ‘pickAlgorithm‘)
The latest version of conflict+docker+mysql8 deployment tutorial
How important is 5g dual card and dual access?
What if redis breaks down?
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
梅科尔工作室-小熊派开发笔记2
2022/7/22 训练日志
13. Roman to Integer罗马数字转整数
How to solve the problem that the solid state disk cannot be found when installing win11?