当前位置:网站首页>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 .
边栏推荐
- 语义分割学习笔记(一)
- Topology architecture of the minimum deployment of tidb cluster
- Steps for Navicat to create a new database
- 工程师评测 | RK3568开发板上手测试
- Common English abbreviations for data analysis (I)
- FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
- 飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
- 17_ Redis_ Redis publish subscription
- 数据分析思维分析方法和业务知识——业务指标
- TiDB混合部署拓扑
猜你喜欢
工程师评测 | RK3568开发板上手测试
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
党史纪实主题公益数字文创产品正式上线
Map介绍
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
03_線性錶_鏈錶
07_哈希
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
随机推荐
Map介绍
Real estate market trend outlook in 2022
Internet Explorer officially retired
YOLOV5 代码复现以及搭载服务器运行
Niuke Practice 101
. Net core logging system
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
15_ Redis_ Redis. Conf detailed explanation
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
2021-2022學年編譯原理考試重點[華僑大學]
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
你不知道的Set集合
How does the computer set up speakers to play microphone sound
. Solution to the problem of Chinese garbled code when net core reads files
07_哈希
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
LeetCode刷题——去除重复字母#316#Medium
Download blender on Alibaba cloud image station
PHP method to get the index value of the array item with the largest key value in the array