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 ...
随机推荐
- 【05】JSON笔记
[05]笔记 尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web Services,否则,在普通的 Web 应用中,开发者经常为 XML 的解析伤透 ...
- Java Syntax Specification
Java Syntax Specification Programs <compilation unit> ::= <package declaration>? <imp ...
- IM聊实现客户端之间信息交互需求文档
终于放假啦~之前学习太忙很多知识点都没有写博客,可能自己学会了但没有分享给大家,接下来几天我可能把一些学过的东西整理成博客发出来供大家相互学习交流. 需求分析说明书 HuaXinIM聊软件 潘浩 20 ...
- tyvj2059 元芳看电影
描述 神探狄仁杰电影版首映这天,狄仁杰.李元芳和狄如燕去看电影.由于人实在是太多了,入场的队伍变得十分不整齐,一个人的前面可能会出现并排的好多人.“元芳,这队伍你怎么看?”“大人,卑职看不出这队伍是怎 ...
- Spring的Java配置方式
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1 @Configuration 和 @Bean Spring的Java配置方式是通过 @Configuration ...
- code wars quiz: toInteger
Your task is to program a function which converts any input to an integer. Do not perform rounding, ...
- FreeMarker与Spring MVC 4结合错误:Caused by: java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfiguration
添加spring-context-support的依赖到POM: <!-- spring-context-support --> <!-- https://mvnrepository ...
- Maven使用GitHub项目目录搭建远程仓库
使用GtiHub的项目目录搭建第三方远程仓库,能免除使用服务器搭建Nexus私服,而且空间也是免费的.但是这种方式只适合小规模发布,毕竟搜索和版本控制是一个问题,如果需要更复杂的功能就只能转向Nexu ...
- 最短的计算大数乘法的c程序
#include <stdio.h> char s[99],t[99]; int m,n; void r(int i,int c) { int j=0,k=i; while(k)c+=s[ ...
- AE 创建
using System; using System.Drawing; using System.Runtime.InteropServices; using ESRI.ArcGIS.ADF; usi ...