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

最新文章

  1. Cesium应用篇:3控件(6) FullScreen/ VR / Home
  2. git ignore
  3. css定位之z-index问题分析
  4. 异常处理__try{}__except(EXCEPTION_EXECUTE_HANDLER){}
  5. VS为VC++添加UAC控制(VC程序默认管理员运行)
  6. strange error encountered today in ROS
  7. iOS 取得单张系统图片
  8. 如何将一个IP地址移出PBL,不然,国内邮件服务器不能正常发送国外邮件哟。
  9. C#实现Ruby的负数索引器
  10. 微服务配置内容《网上copy》=========》如何创建一个高可用的服务注册中心
  11. IEC104协议规约解析
  12. impala操作hase、hive
  13. MyBatis笔记----MyBatis 入门经典的两个例子: XML 定义与注解定义
  14. 指导手册02:伪分布式安装Hadoop(ubuntuLinux)
  15. python 全栈开发,Day123(图灵机器人,web录音实现自动化交互问答)
  16. 使用JAVA API 解析ORC File
  17. SpringBoot2 web
  18. Java:集合,Arrays工具类用法
  19. Python爬虫(六)
  20. 【51nod-1315】合法整数集(数位)

热门文章

  1. [国家集训队2010]小Z的袜子
  2. 别墅房间 CodeVS原创
  3. Extended symmetrical multiprocessor architecture
  4. strace oracle
  5. webpack打包的基础原理-打包后的文件解读
  6. Android GIS开发系列-- 入门季(4) GraphicsLayer的点击查询要素
  7. windows下检測文件改变
  8. [容斥原理] hdu 1796 How many integers can you find
  9. React Native 中的component 的生命周期
  10. starUML 2.5.1 for mac