当前位置:网站首页>Leetcode question brushing - parity linked list 328 medium
Leetcode question brushing - parity linked list 328 medium
2022-07-02 15:28:00 【Fire breathing dragon and water arrow turtle】
Discussion and source code of odd and even linked list
The title of the parity linked list is shown in the figure below , This question belongs to the linked list and search type , Mainly investigate the use of search methods and the understanding of linked list structure . The title of this article, the author thought 2 Methods , They are recursive search method and double pointer method , Where recursive search uses Java Compiling , The double pointer method uses Python Compiling , Of course, this may not be the optimal solution , I also hope you guys can give a faster algorithm .
I think this problem can be solved by using the idea of recursive search , First, judge whether the list is empty , If yes, return null directly . Then initialize to get odd number of header nodes and even number of header nodes , And record the odd number of header nodes , Start calling recursive functions to search , Inside a recursive function , It mainly determines whether the even node or the next node after the even node is empty , If yes, it will directly return to the even node ; Otherwise, point the even node to the next node of the odd node , Then point the odd node to the next node of the even node , And continue to recursively call until the end of the search and return the final linked list results . Then according to this idea, our Java The code is as follows :
# Fire breathing dragon and water arrow turtle
class Solution {
public ListNode oddEvenList(ListNode head) {
if(head == null){
return null;
}
ListNode one = head;
ListNode two = head.next;
ListNode twoHead = two;
rev(one,two).next = twoHead;
return head;
}
private ListNode rev(ListNode one,ListNode two){
if(one.next == null || one.next.next == null)
return one;
one.next = two.next;
two.next = one.next.next;
return rev(one.next,two.next);
}
}

obviously , We see that the recursive search method works well , At the same time, the double pointer method can also be used to solve . First, judge whether the list is empty , If yes, it directly returns null , Then record even nodes , And get odd nodes and even nodes , Then traverse the even nodes , When the even node is not empty and the next node under the even node is not empty , Point the odd node to the next node of the even node , Move the odd node to the next position ; Point the even node to the next node of the odd node , Move even nodes to the next location . Finally, until the end of the traversal , Point the odd linked list to the even linked list , It is the merged result and returns . So according to this idea, we can solve , Here is Python Code :
# Fire breathing dragon and water arrow turtle
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
if(not head):
return head
evenHead = head.next
odd = head
even = evenHead
while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = evenHead
return head

As a result Java The efficiency of this version of recursive search method is good , and Python The speed of the version of the double pointer method is also OK , But there should be more ways to further speed up , I hope friends can give me more advice , Thank you very much .
边栏推荐
- 14_Redis_乐观锁
- HUSTPC2022
- 06_栈和队列转换
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- 19_ Redis_ Manually configure the host after downtime
- Application of CDN in game field
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- 05_队列
- 飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
- Implementation of n queen in C language
猜你喜欢

03_線性錶_鏈錶

Semantic segmentation learning notes (1)

工程师评测 | RK3568开发板上手测试

Steps for Navicat to create a new database

Mavn 搭建 Nexus 私服
![[c voice] explain the advanced pointer and points for attention (2)](/img/fb/515e25899bd9a2905ee63cb041934a.png)
[c voice] explain the advanced pointer and points for attention (2)

Data analysis thinking analysis methods and business knowledge - business indicators

LeetCode刷题——去除重复字母#316#Medium

List集合&UML图

18_Redis_Redis主从复制&&集群搭建
随机推荐
21_Redis_浅析Redis缓存穿透和雪崩
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
Case introduction and problem analysis of microservice
04_ Stack
Common English abbreviations for data analysis (I)
记一次面试
05_队列
XML配置文件
损失函数与正负样本分配:YOLO系列
Steps for Navicat to create a new database
党史纪实主题公益数字文创产品正式上线
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
11_ Redis_ Hyperloglog_ command
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
03.golang初步使用
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
[solution] educational codeforces round 82
YOLOV5 代码复现以及搭载服务器运行
语义分割学习笔记(一)