328 Odd Even Linked List 奇偶链表
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 ...
详见:https://leetcode.com/problems/odd-even-linked-list/description/
C++:
/**
* 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) {
if(!head||!head->next)
{
return head;
}
ListNode *pre=head;
ListNode *cur=head->next;
while(cur&&cur->next)
{
ListNode *tmp=pre->next;
pre->next=cur->next;
cur->next=cur->next->next;
pre->next->next=tmp;
cur=cur->next;
pre=pre->next;
}
return head;
}
};
参考:https://www.cnblogs.com/grandyang/p/5138936.html
328 Odd Even Linked List 奇偶链表的更多相关文章
- [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)
每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- <LeetCode OJ> 328. Odd Even Linked List
328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...
- 【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
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 1->2->3->4->5->NULL, return 1->3->5->2->4->NULL. 就是将序号为单数的放在前面,而 ...
- 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
一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...
- 【leetcode】328. Odd Even Linked List
题目如下: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...
随机推荐
- pressure coeffcient of a wing/blade
software: CFD POST ANSYS menu bar, select Tools > Macro Calculator. \ correction: Ref pressure is ...
- BZOJ 2274 [Usaco2011 Feb]Generic Cow Protests
[题解] 很容易可以写出朴素DP方程f[i]=sigma f[j] (sum[i]>=sum[j],1<=j<=i). 于是我们用权值树状数组优化即可. #include<c ...
- Poor Hanamichi
Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- BNUOJ 1206 A Plug for UNIX
A Plug for UNIX Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Origina ...
- noip模拟赛 立方数2
题目描述LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数,例如1,8,27就是最小的3个立方数.LYK还定义了一个数叫“立方差数”,若一个数可以被写作是两个立 ...
- Linux 安装 RabbitMQ
转载文章,地址:https://www.cnblogs.com/uptothesky/p/6094357.html 侵删!
- mysql 中间件
http://f.dataguru.cn/thread-543718-1-1.html mysql-proxy是官方提供的mysql中间件产品可以实现负载平衡,读写分离,failover等,但其不支持 ...
- 从零单排入门机器学习:Octave/matlab的经常使用知识之矩阵和向量
Octave/matlab的经常使用知识之矩阵和向量 之前一段时间在coursera看了Andrew ng的机器学习的课程,感觉还不错.算是入门了.这次打算以该课程的作业为主线,对机器学习基本知识做一 ...
- 扩展VirtualBox中的centos硬盘大小
一.克隆文件 我之前安装的时候建的是centos 6.3.可是后来空间不够,没办法,又不想重装centos.由于好多东西要配置,特麻烦,所以先想到了使用resize命令,可是在win8中运行D:\Pr ...
- android 请求网络异步载入
/** * 封装ProecssDialog对话框 * */ public class LoadDialog extends ProgressDialog { private String title ...