https://leetcode.com/problems/odd-even-linked-list/description/
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 1:
Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL
Example 2:
Input: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->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 ...
符合直觉的想法是,先遍历一遍找出奇数的节点。然后再遍历一遍找出偶数节点,最后串起来。
但是有两个问题,如果不修改节点的话,需要借助额外的空间,空间复杂度是N。如果修改的话,会对第二次遍历(遍历偶数节点)造成影响。
因此可以采用一种做法: 遍历一次,每一步同时修改两个节点就好了,这样就可以规避上面两个问题。
-
用虚拟节点来简化操作
-
循环的结束条件设置为
odd && odd.next && even && even.next
, 不应该是odd && even
, 否则需要记录一下奇数节点的最后一个节点,复杂了操作
/*
* @lc app=leetcode id=328 lang=javascript
*
* [328] Odd Even Linked List
*
* https://leetcode.com/problems/odd-even-linked-list/description/
*
* algorithms
* Medium (48.22%)
* Total Accepted: 137.6K
* Total Submissions: 284.2K
* Testcase Example: '[1,2,3,4,5]'
*
* 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 1:
*
*
* Input: 1->2->3->4->5->NULL
* Output: 1->3->5->2->4->NULL
*
*
* Example 2:
*
*
* Input: 2->1->3->5->6->4->7->NULL
* Output: 2->3->6->7->1->5->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.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var oddEvenList = function(head) {
if (!head || !head.next) return head;
const dummyHead1 = {
next: head
}
const dummyHead2 = {
next: head.next
}
let odd = dummyHead1.next;
let even = dummyHead2.next;
while(odd && odd.next && even && even.next) {
const oddNext = odd.next.next;
const evenNext = even.next.next;
odd.next = oddNext;
even.next = evenNext;
odd = oddNext;
even = evenNext;
}
odd.next = dummyHead2.next;
return dummyHead1.next;
};