leetcode 328 Odd Even Linked List(调整链表使得奇数位置的元素位于偶数位置元素之前)
2016-05-27 15:31
399 查看
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. 题目的大概意思是:把链表中奇数位置的元素放在偶数位置的元素之前。
ListNode* oddEvenList(ListNode* head) { if(head == NULL) return head; ListNode *odd = head; ListNode *evenHead = head->next; ListNode *even = evenHead; while(even && even->next) { odd->next = even->next; even->next = even->next->next; odd = odd->next; even = even->next; } odd->next = evenHead; return head; }
相关文章推荐
- 在Ubuntu 14.04 64bit上安装OpenResty 1.9.7.4
- about modern operating system lecture
- [Spring] - Property注入
- 为github帐号添加SSH keys
- Android-PullToRefresh
- 移动无线网络演进历史
- rsync 命令
- 史上最全APP推广渠道
- 二维码推广网站
- c库函数——字符串函数
- SpringMVC+WebSocket整合
- Java线程详解
- 在SDK中打开其他接入应用的解决方案
- JavaScript删除数组重复元素的5个高效算法
- 类设计感悟
- 写出smarty模板的特点
- 非常酷炫的Bootstrap图片轮播动画
- 选择香港虚拟主机空间的优势
- C++单例模式getInstance()——设计模式(1)
- Ti:关于类变量(静态成员变量)的作用域,以及生命周期