LeetCode刷题(C++)——Odd Even Linked List(Medium)
来源:互联网 发布:套淘宝保证金如何秒回 编辑:程序博客网 时间:2024/06/06 17:54
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 ...
/** * 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){ return NULL; } ListNode* head1=NULL,*tail1=NULL,*head2=NULL,*tail2=NULL; for(int i=1;head;++i,head=head->next){ if(i & 1){ if(tail1){ tail1=tail1->next=head; } else{ head1=tail1=head; } } else if(tail2){ tail2=tail2->next=head; } else{ head2=tail2=head; } } if(tail1){ tail1->next=head2; } else{ head1=head2; } if(tail2){ tail2->next=NULL; } return head1; }};
阅读全文
0 0
- LeetCode刷题(C++)——Odd Even Linked List(Medium)
- [leetcode]328. Odd Even Linked List [medium]
- LeetCode 328. Odd Even Linked List (Medium)
- LeetCode—328. Odd Even Linked List
- Leetcode 328. Odd Even Linked List (Medium) (cpp)
- LeetCode刷题: Odd Even Linked List
- leetcode——328——Odd Even Linked List
- 328. Odd Even Linked List #Medium
- leetcode 328 Odd Even Linked List(难易度:Easy)
- LeetCode之旅(15)-Odd Even Linked List
- leetcode之328. Odd Even Linked List(C++解法)
- leetcode Odd Even Linked List
- [leetcode]Odd Even Linked List
- [Leetcode]Odd Even Linked List
- LeetCode Odd Even Linked List
- Leetcode: Odd Even Linked List
- LeetCode:Odd Even Linked List
- [LeetCode] Odd Even Linked List
- Kettle安装使用教程(未完待续)
- java https请求
- Android 高仿微信图片查看花式效果
- H2642PES
- UNet实战系列(二)
- LeetCode刷题(C++)——Odd Even Linked List(Medium)
- Android-程序、进程和线程
- 你知道吗?wb是默认开启兼容性视图的
- Linux 添加代理
- MapReduce编程练习(四),统计多个输入文件学生的平均成绩,
- 优化中的subgradient方法
- leetcode刷题阶段总结
- 188. Best Time to Buy and Sell Stock IV
- sealed(C# )