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

就是将序号为单数的放在前面,而序号为偶数的放在后面

我的方法是讲序号为偶数的的插入到链表末尾

 /**
* 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) return head;//空链表返回
ListNode* last = head;
int cnt = ;
for(; last->next; last = last->next, ++cnt);
if(cnt < ) return head;//链表长度小于3返回
ListNode* now = head;
ListNode* end = last; for(int i = ; i< cnt/; now = now->next, ++i){ ListNode* next = now->next;
now->next = next->next; end->next = next;
next->next = NULL; end = next; }
return head;
}
};

Leetcode 328 Odd Even Linked List 链表的更多相关文章

  1. [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)

    每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...

  2. (链表) leetcode 328. Odd Even Linked List

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

  3. LeetCode 328. Odd Even Linked List

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

  4. Java [Leetcode 328]Odd Even Linked List

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

  5. 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 ...

  6. &lt;LeetCode OJ&gt; 328. Odd Even Linked List

    328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...

  7. 【一天一道LeetCode】#328 Odd Even Linked List

    一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...

  8. 【LeetCode】328. Odd Even Linked List 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【Leetcode】 328. Odd Even Linked List

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

随机推荐

  1. HTML5和CSS3基础教程(第8版)-读书笔记(2)

    第7章 CSS构造模块 7.1 构造样式规则 样式表中包含了定义网页外观的规则.样式表中的每条规则都有两个主要部分:选 择 器(selector) 和 声 明 块(declaration block) ...

  2. HttpURLConnection请求网络数据的Post请求

    //--------全局变量----------- //注册Url    private String urlPath="http://101.200.142.201:8080/VideoP ...

  3. iOS.ReactNative-2-bridge-and-react-native-app-execution

    Bridge and React Native App Execution 基于0.18.1 Async batched bridge used to communicate with the Jav ...

  4. Win7中不能调试windows service

    多年前玩过一次windows service,觉得挺简单的. 这次工作要维护产品中的windows service,发现不是那么简单,vs附加调试器的窗体中无法找到windows service进程. ...

  5. Odoo10尝鲜: 退货

    Odoo sale / purchase 在 v9 改进之后, 开立发票的入口 不再像之前的版本,有多个来源,例如 订单 交货单 记工单 分析分录     现在只有一个入口,只需要在 订单上开票,这样 ...

  6. vi/vim学习

    1.vi3中模式 一般模式.编辑模式.命令模式2.快捷操作 i.I插入:i在目前的光标所在处插入文字:I(大写i)在行首插入 a.A 增加:a由光标所在的下一个字开始输入:A由光标所在行的最后增加 o ...

  7. 安卓奇葩问题之:设置webView超时

    我只想说:what a fucking day! 今天要做一个webView的超时功能,于是开始百度,一看貌似很简单啊,于是开始copy了下面的代码. import java.util.Timer; ...

  8. 【状压dp】【bitset】bzoj1688 [Usaco2005 Open]Disease Manangement 疾病管理

    vs(i)表示患i这种疾病的牛的集合. f(S)表示S集合的病被多少头牛患了. 枚举不在S中的疾病i,把除了i和S之外的所有病的牛集合记作St. f(S|i)=max{f(S)+((St|vs(i)) ...

  9. C++ set使用

    C++ set使用 实际上c++ STL中的set是的实现和C++ STL中的map的实现的底层数据结构是一样的,如果我们不在考虑红黑树中的卫星数据,而只是关键字,那么同样不允许key值得重复,那么就 ...

  10. Shiro-多Realm验证

    1.多Realm验证 存在这样一种场景,同一个密码可能在MqSQL中存储,也可能在Oracle中存储,有可能MqSQL中使用的是MD5加密算法,而Oracle使用SHA1加密算法.这就需要有多个Rea ...