作者: 负雪明烛
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. oracle imp dmp
  2. 关于page object(PO)模型的介绍
  3. 前端开发利器webStorm 3.0配置使用
  4. 软件工程---团队作业四
  5. 《LINUX内核设计与实现》第五章学习总结
  6. 图文解说:Nginx+tomcat配置集群负载均衡
  7. WPF ListBox样式去掉默认选中效果
  8. QT学习笔记(十二):透明窗体设置
  9. 学Python可以用来干什么?就业情况究竟怎么样?
  10. kbmmw 与extjs 通过JSON Base64 显示图片
  11. python爬虫可以爬取哪些有用的东西_Python爬虫系列(十三) 用selenium爬取京东商品...
  12. Skype for Business Server 2015-01-基础环境-准备
  13. 淡雅简洁商业汇演商业计划书PPT模板
  14. android Google Advertising ID 如何重置
  15. Apollo详解之canbus模块——车辆底层协议调试
  16. 2020最新的黑马Java分享,包含课件,资料,项目,教程全部免费分享,需要的赶紧去收藏吧
  17. ※设计模式※→☆创建型模式☆============Builder模式(五)
  18. 【数据库】学生档案管理系统(续)
  19. MySQL不会丢失数据的秘密,就藏在它的 7种日志里
  20. spss实战案例----分析多个变量与因变量之间是否存在关系,方差分析

热门文章

  1. 手机: 1581 0812 176
  2. dhu 6 获取AOE网的关键路径
  3. SQL WHERE AND OR
  4. windows7系统无法开机显示丢失volmgrx.sys驱动文件
  5. 餐中餐(5)Lucene--存储文件加载(Part 1: Segments加载)
  6. c语言发邮件,c语言利用SMTP协议发送邮件
  7. Verdi非常实用技巧
  8. IDEA中Java项目删除Web框架后无法再次添加Web框架解决办法
  9. 哲哲打游戏 (25 分)
  10. 【图片新闻】法国计划加快天基激光武器的研发与部署