328 Odd Even Linked List 奇偶链表
2024-09-04 09:21:45
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
最新文章
- Cesium应用篇:3控件(6) FullScreen/ VR / Home
- git ignore
- css定位之z-index问题分析
- 异常处理__try{}__except(EXCEPTION_EXECUTE_HANDLER){}
- VS为VC++添加UAC控制(VC程序默认管理员运行)
- strange error encountered today in ROS
- iOS 取得单张系统图片
- 如何将一个IP地址移出PBL,不然,国内邮件服务器不能正常发送国外邮件哟。
- C#实现Ruby的负数索引器
- 微服务配置内容《网上copy》=========》如何创建一个高可用的服务注册中心
- IEC104协议规约解析
- impala操作hase、hive
- MyBatis笔记----MyBatis 入门经典的两个例子: XML 定义与注解定义
- 指导手册02:伪分布式安装Hadoop(ubuntuLinux)
- python 全栈开发,Day123(图灵机器人,web录音实现自动化交互问答)
- 使用JAVA API 解析ORC File
- SpringBoot2 web
- Java:集合,Arrays工具类用法
- Python爬虫(六)
- 【51nod-1315】合法整数集(数位)
热门文章
- [国家集训队2010]小Z的袜子
- 别墅房间 CodeVS原创
- Extended symmetrical multiprocessor architecture
- strace oracle
- webpack打包的基础原理-打包后的文件解读
- Android GIS开发系列-- 入门季(4) GraphicsLayer的点击查询要素
- windows下检測文件改变
- [容斥原理] hdu 1796 How many integers can you find
- React Native 中的component 的生命周期
- starUML 2.5.1 for mac