作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


目录

  • 题目描述
  • 题目大意
  • 解题方法
  • 日期

题目地址:https://leetcode.com/problems/odd-even-linked-list/description/

题目描述

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.

Example:
Given 1->2->3->4->5->NULL,
return 1->3->5->2->4->NULL.

Note:

  1. The relative order inside both the even and odd groups should remain as it was in the input.
  2. The first node is considered odd, the second node even and so on …

题目大意

把一个链表的奇数序号的节点放在前面,偶数序号的节点放在后面。注意使用O(n)的时间复杂度和O(1)的空间。

解题方法

我的想法很朴素。我只用弄出来两条链不就好了吗?如果是奇数节点放到奇链,如果是偶数节点就放到偶链。最后,把偶链放到奇链的后面就好了。

注意,偶链的末尾指针要设置成空,已让单链表终止。

比如对于用例[1,2,3],奇数链是1->3,偶链是2,而遍历完成后的偶链2仍然指向3的,所以死循环了。把尾指针设置成空就能终止了。

Python代码如下:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = Noneclass Solution(object):def oddEvenList(self, head):""":type head: ListNode:rtype: ListNode"""odd = ListNode(0)even = ListNode(0)oddHead, evenHead = odd, evenindex = 0while head:if index & 1 == 0:odd.next = headodd = odd.nextelse:even.next = headeven = even.nexthead = head.nextindex += 1even.next = Noneodd.next = evenHead.nextreturn oddHead.next

C++代码如下,不过由于在函数中声明了普通指针而没有delete,会造成内存泄漏,leetcode能通过,但是面试的时候要小心。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {public:ListNode* oddEvenList(ListNode* head) {ListNode* oddHead = new ListNode(0);ListNode* evenHead = new ListNode(0);ListNode* odd = oddHead;ListNode* even = evenHead;int index = 1;while (head) {if (index & 1) {odd->next = head;odd = odd->next;} else {even->next = head;even = even->next;}head = head->next;++index;}if (even->next) even->next = nullptr;if (evenHead->next)odd->next = evenHead->next;return oddHead->next;}
};

日期

2018 年 3 月 15 日 —— 雾霾消散,春光明媚
2019 年 3 月 23 日 —— 一年之后重刷此题,还是还有点生疏

【LeetCode】328. Odd Even Linked List 解题报告(Python C++)相关推荐

  1. [leetcode]328. Odd Even Linked List

    题目 Given a singly linked list, group all odd nodes together followed by the even nodes. Please note ...

  2. leetcode 328. Odd Even Linked List | 328. 奇偶链表(Java)

    题目 https://leetcode.com/problems/odd-even-linked-list/ 题解 要注意的是,因为只将偶数位置的节点 append 到最后,所以用于判断停止的 tai ...

  3. LeetCode第45场双周赛-解题报告

    LeetCode第45场双周赛-解题报告 A. 唯一元素的和 原题链接 https://leetcode-cn.com/problems/sum-of-unique-elements/ 解题思路 因为 ...

  4. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  5. LeetCode Notes_#206 Reverse Linked List(C++,Python)

    LeetCode Notes_#206 Reverse Linked List(C++,Python) LeetCode Linked List  Contents 题目 思路 思考 解答 C++ P ...

  6. LeetCode 206 Reverse Linked List 解题报告

    题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...

  7. LeetCode 876 Middle of the Linked List 解题报告

    题目要求 Given a non-empty, singly linked list with head node head, return a middle node of linked list. ...

  8. Leet Code OJ 328. Odd Even Linked List [Difficulty: Easy]

    题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note ...

  9. leetcode 214. 最短回文串 解题报告

    给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...

最新文章

  1. 这些Java8官方挖的坑,你踩过几个?
  2. python使用openweathermap API获取全世界主要城市天气信息
  3. JeeSite 4.0 说说前端的那些事
  4. Python——Django框架——django-simple-captcha(验证码)
  5. 小学奥数 7830 求小数的某一位 python
  6. y电容如何选型_高压并联补偿电容器基础及运行管理
  7. 大型网站架构系列:负载均衡详解(4)
  8. 带徽标的Bootstrap 3 Navbar
  9. 免费下载“百度文库”文档方法,简单而强大~
  10. c语言中1和0什么意思啊,精讲LOOKUP公式中1和0的含义,这1500字的详细解析值得收藏一份...
  11. 利用gitlab pages和hexo搭建一个个人博客
  12. [phyton]文件的简单读写练习
  13. sticky android,Sticky Warriors
  14. 投简历的格式(第一分简历)
  15. weblogic-cve_2020_2883漏洞复现
  16. java基础小记_[Java教程]Java基础学习小记
  17. pdf转ppt怎么转?
  18. 【机器学习】HMM模型
  19. origin画图——同一图中多组数据
  20. Android 版 Facebook 登录

热门文章

  1. iphone怎么更新9.0系统更新服务器,iOS 9 推送前你必须知道的几件事:iOS 9 升级指南...
  2. KENALLRYLLDKDD|359821-54-8
  3. 屏蔽百度无孔不入的垃圾广告
  4. android登陆按钮图片素材,Android精美登录界面设计
  5. 股票数据爬虫(Scrapy框架与requests-bs4-re技术路线)
  6. 一种防山火在线监测装置
  7. MySQL免安装版下载及安装
  8. SQL中with as用法
  9. 中国医疗保健分析测试服务行业市场供需与战略研究报告
  10. BCG 使用之CBCGPColorDialog控件