Leetcode328 奇偶链表

思路:分别处理奇偶,保存奇偶的第一个和最后一个节点,注意最后链接的时候需要把偶数的next去掉再拼接不然就成环了

python 复制代码
class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        if not head or not head.next or not head.next.next:
            return head

        first_odd = head
        first_even = head.next
        last_odd = head
        last_even = head.next
        current_node = first_even.next
        node_num = 3

        while current_node:
            next_node = current_node.next
            if node_num % 2 == 1:
                last_odd.next = current_node
                last_odd=current_node
            else:
                last_even.next = current_node
                last_even=current_node
            current_node = next_node
            node_num += 1
        last_odd.next=first_even
        last_even.next=None
        return first_odd
相关推荐
无语的小歪歪1 分钟前
ATF 智测 1.0
python·测试工具·集成测试
czq_26867194877 分钟前
Python打卡第45天
开发语言·python
java_prinln9 分钟前
9. Agent 的长短期记忆系统怎么做的?记忆是怎么存的?粒度是多少?怎么用的?
数据库·python·agent
FOAF-lambda13 分钟前
Python实战:基于OpenCV与单调栈,精准定位PDF中最大的空白区域
python·opencv·pdf
terry60019 分钟前
企业数据查询接口防爬实战:图形验证码服务端校验接入与限流设计(附Python)
python·信息与通信·图形验证码·反爬虫·人机验证·行为验证码·滑动拼图验证
量化炼金 (CodeAlchemy)20 分钟前
【Python】pandas vs polars 简单性能对比测试
开发语言·python·pandas
学习论之费曼学习法22 分钟前
无人值守AI内容矩阵实战(7):Playwright 发布器避坑——编辑器竞态、索引延迟与风控
python·线性代数·矩阵·编辑器·浏览器自动化·playwright·踩坑记录
wuyk55527 分钟前
PyQt6-第06讲-主窗口框架
python·pyqt
ss.li29 分钟前
rppg相关论文阅读笔记10.22
论文阅读·笔记·python
tryCbest29 分钟前
Python之Flask项目部署到阿里云
python·阿里云·flask