当前位置:网站首页>LeetCode刷题——奇偶链表#328#Medium
LeetCode刷题——奇偶链表#328#Medium
2022-07-02 12:04:00 【喷火龙与水箭龟】
奇偶链表的思路探讨与源码
奇偶链表的题目如下图,该题属于链表类和搜索类型的题目,主要考察对于搜索方法的使用和链表结构的理解。本文的题目作者想到2种方法,分别是递归搜索方法和双指针方法,其中递归搜索使用Java进行编写,而双指针方法使用Python进行编写,当然这可能不是最优的解法,还希望各位大佬给出更快的算法。
本人认为该题目可以使用递归搜索方法的思路进行解决,首先判断链表是否为空,如果是则直接返回空。然后初始化得到奇数的头节点和偶数的头节点,并记录奇数的头节点,开始调用递归函数进行搜索,在递归函数内部,主要是判断偶数节点或者偶数后的下一个节点是否为空,如果是就直接返回偶数节点;否则就将偶数节点指向奇数节点的下一个节点,再将奇数节点指向偶数节点的下一个节点,并继续递归调用直到搜索结束并返回最终的链表结果。那么按照这个思路我们的Java代码如下:
#喷火龙与水箭龟
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);
}
}

显然,我们看到递归搜索方法的效果还不错,同时还可以使用双指针的方法解决。首先判断链表是否为空,如果是就直接返回空,然后记录偶数节点,并获取出奇数节点和偶数节点,再对偶数节点进行遍历,当偶数节点不为空并且偶数节点下一个节点也不为空的时候,将奇数节点指向偶数节点的下一个节点,将奇数节点移动到下一个位置;将偶数节点指向奇数节点的下一个节点,将偶数节点移动到下一个位置。最终直到遍历结束,将奇数链表指向偶数链表,即为合并后的结果并返回。所以按照这个思路就可以解决,下面是Python代码:
#喷火龙与水箭龟
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

从结果来说Java版本的递归搜索方法的效率还不错,而Python版本的双指针方法的速度也还可以,但应该是有更多的方法可以进一步提速的,希望朋友们能够多多指教,非常感谢。
边栏推荐
- Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
- QML pop-up frame, customizable
- 哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
- 04_ Stack
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- 16_Redis_Redis持久化
- 如何对 TiDB 进行 TPC-C 测试
- 07_ Hash
- N皇后问题的解决
- Engineer evaluation | rk3568 development board hands-on test
猜你喜欢

XML配置文件

学习使用php将时间戳转换为大写日期的方法代码示例

02_线性表_顺序表

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测

N皇后问题的解决

Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July

05_ queue

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?

20_ Redis_ Sentinel mode
随机推荐
搭建自己的语义分割平台deeplabV3+
Common English abbreviations for data analysis (I)
Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
AtCoder Beginner Contest 254
MySQL -- Index Optimization -- order by
Internet Explorer officially retired
06_ Stack and queue conversion
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
20_ Redis_ Sentinel mode
08_ strand
. Net core logging system
语义分割学习笔记(一)
Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
Application and practice of Jenkins pipeline
. Solution to the problem of Chinese garbled code when net core reads files
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
Tidb data migration scenario overview
HUSTPC2022