leetcode-328. Odd Even Linked List
来源:互联网 发布:iphone4 ios7.1.2优化 编辑:程序博客网 时间:2024/05/20 05:53
链接:https://leetcode.com/problems/odd-even-linked-list/
题目:
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 …
Credits:
Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
思路:题目意思是找奇偶链表,奇数点放前面,偶数点放后面,这里是指节点不是值。
代码如下:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode oddEvenList(ListNode head) { if(head==null) return head; ListNode even=head.next; ListNode odd=head; ListNode temp=even; while(even!=null&&even.next!=null){ odd.next=even.next; odd=odd.next; even.next=odd.next; even=even.next; } odd.next=temp; return head; }}
- 328. Odd Even Linked List leetcode(list)
- LeetCode—328. Odd Even Linked List
- LeetCode 328. Odd Even Linked List Java
- Leetcode 328. Odd Even Linked List
- <LeetCode><Easy> 328. Odd Even Linked List
- [leetcode] - 328. Odd Even Linked List
- <LeetCode OJ> 328. Odd Even Linked List
- 328. Odd Even Linked List LeetCode
- leetCode 328. Odd Even Linked List
- Leetcode: 328. Odd Even Linked List(JAVA)
- LeetCode--328. Odd Even Linked List
- LeetCode 328. Odd Even Linked List
- LeetCode 328. Odd Even Linked List
- [LeetCode]328. Odd Even Linked List
- 【LeetCode】328. Odd Even Linked List
- LeetCode 328. Odd Even Linked List
- leetcode 328. Odd Even Linked List
- [leetcode]328. Odd Even Linked List
- Oracle基础
- 山东省第七届ACM省赛------Julyed
- Android apk打包过程
- 滚动条样式的设定(ipad也好用)
- POJ 2524 Ubiquitous Religions (简单并查集,三种方式)
- leetcode-328. Odd Even Linked List
- poj3207 Ikki's Story IV - Panda's Trick
- hdu 1452(因子和+积性函数)
- [6-7]考试总结
- Android屏幕适配
- 使用GCC和Eclipse在Mac OS X下搭建nRF5系列BLE芯片开发环境
- 数书九章 内容
- ISCC2016 BASE 第二题好长的字符串
- java多线程与内存可见性