当前位置:网站首页>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 .
边栏推荐
- Beijing rental data analysis
- 05_队列
- Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
- Application and practice of Jenkins pipeline
- 【网络安全】网络资产收集
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
- 17_Redis_Redis发布订阅
- 语义分割学习笔记(一)
- 使用 TiUP 部署 TiDB 集群
- 数据分析思维分析方法和业务知识——业务指标
猜你喜欢

FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA

Evaluation of embedded rz/g2l processor core board and development board of Feiling

How to choose a third-party software testing organization for automated acceptance testing of mobile applications

面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?

08_ 串

Solve the problem of frequent interruption of mobaxterm remote connection

02_线性表_顺序表

Let your HMI have more advantages. Fet-g2ld-c core board is a good choice

6.12 企业内部upp平台(Unified Process Platform)的关键一刻

05_队列
随机推荐
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
. Net core logging system
Force deduction solution summary 2029 stone game IX
14_Redis_乐观锁
AtCoder Beginner Contest 254
数据分析常见的英文缩写(一)
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
N皇后问题的解决
08_ 串
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
04_ Stack
15_Redis_Redis.conf详解
Record an interview
03.golang初步使用
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
07_哈希
Real estate market trend outlook in 2022
Recommended configuration of tidb software and hardware environment
Map介绍