题目:

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.

将偶数位置的结点放在左边,奇数位置的结点放在右边

代码:

/**
* 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==NULL || head->next==NULL) return head;
ListNode *odd = head,*evenHead = head->next, *even = evenHead;
while(even != NULL && even->next != NULL)
{
odd->next = even->next;
odd = odd->next;
even->next = odd->next;
even = even->next;
}
odd->next = evenHead;
return head;
}
};

最新文章

  1. WebStorm和sublime上使用git连接github(转)
  2. hibernate中设置BigDeCimal的精度
  3. net 的单元测试 初学
  4. Linux下建立Nexus私服
  5. Android开发之android:process=":remote"
  6. kali linux 2.0 折腾笔记
  7. nodejs 微信中使用file组件上传图片在某些机型上点击无反应
  8. (CodeForces )540B School Marks 贪心 (中位数)
  9. C++ 使用string一行一行读取文件
  10. iOS bug 之 H5 页面没有弹出提示框
  11. 搜索引擎的缓存(cache)机制
  12. 利用Grafana展示zabbix数据
  13. python 调用c语言函数
  14. elasticsearch之入门hello(java)一
  15. 计算属性(computed)、方法(methods)和侦听属性(watch)的区别与使用场景
  16. discuz 不能上传头像提示can not write to the data/tmp folder
  17. 深入出不来nodejs源码-流程总览
  18. robotium 中通过id获取 View 以及进行相应的操作
  19. webBrowser获取取Cookie不全的解决方法
  20. iOS - 上架的APP 生成二维码下载

热门文章

  1. LeetCode——Search in Rotated Sorted Array II
  2. Wix学习整理(6)——安装快捷方式
  3. hdu3496(二维背包)
  4. Knockout应用开发指南 第九章:高级应用举例
  5. Java Executor 框架
  6. 询问任意区间的min,max,gcd,lcm,sum,xor,or,and
  7. ECSHOP如何增加红包序列号字符
  8. Python入门(转)
  9. 对于Web开发来说 8 个最好的跨平台编辑器
  10. iOS Dev (55) 获得本年度、月、日本和其他信息