【LeetCode】328. Odd Even Linked List 解题报告(Python & C++)
作者: 负雪明烛
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:
- The relative order inside both the even and odd groups should remain as it was in the input.
- 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 = None
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
odd = ListNode(0)
even = ListNode(0)
oddHead, evenHead = odd, even
index = 0
while head:
if index & 1 == 0:
odd.next = head
odd = odd.next
else:
even.next = head
even = even.next
head = head.next
index += 1
even.next = None
odd.next = evenHead.next
return 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++)的更多相关文章
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)
每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...
- Java [Leetcode 328]Odd Even Linked List
题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】707. Design Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- LeetCode 328. Odd Even Linked List C#
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- (链表) leetcode 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
随机推荐
- Redis—怎么查看Linux有没有安装Redis,如何启动Redis
1.检测是否有安装redis-cli和redis-server [root@localhost bin]# whereis redis-cli redis-cli: /usr/bin/redis-cl ...
- Pytorch学习笔记08----优化器算法Optimizer详解(SGD、Adam)
1.优化器算法简述 首先来看一下梯度下降最常见的三种变形 BGD,SGD,MBGD,这三种形式的区别就是取决于我们用多少数据来计算目标函数的梯度,这样的话自然就涉及到一个 trade-off,即参数更 ...
- 学习java 7.19
学习内容: 接口的组成中加入了默认方法,静态方法,私有方法 接口中默认方法:public default 返回值类型 方法名(参数列表){ } public default void show() ...
- 大数据学习day15----第三阶段----scala03--------1.函数(“_”的使用, 函数和方法的区别)2. 数组和集合常用的方法(迭代器,并行集合) 3. 深度理解函数 4 练习(用java实现类似Scala函数式编程的功能(不能使用Lambda表达式))
1. 函数 函数就是一个非常灵活的运算逻辑,可以灵活的将函数传入方法中,前提是方法中接收的是类型一致的函数类型 函数式编程的好处:想要做什么就调用相应的方法(fliter.map.groupBy.so ...
- 一道题目学ES6 API,合并对象id相同的两个数组对象
var arr2=[{id:1,name:'23'}] var arr1=[{id:1,car:'car2'}] const combined = arr2.reduce((acc, cur) =&g ...
- Ubantu nodejs卸载与二进制安装
#apt-get 卸载 sudo apt-get remove --purge npm sudo apt-get remove --purge nodejs sudo apt-get remove - ...
- Android获取通知栏的高度
1 public static int getStatusBarHeight(Context context){ 2 Class<?> c = null; 3 ...
- JQuery 和 CSS 等选择器:
JQuery 选择器: CSS 选择器:
- SpringMVC中@RestController和@Controller的区别
在使用SpringMVC时,对于Controller中的注解@RestController和@Controller需要我们区分清楚 @RestController注解相当于@ResponseBody和 ...
- contrller层的编码设设计流程以及详细配置
/** 实际开发中遵循一个规律:自己写的类使用注解,系统提供的类使用配置文件 1.书写controller类----->配置springmvc.xml-------->配置web ...