-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode-328-OddEvenLinkedList.js
72 lines (66 loc) · 1.52 KB
/
leetcode-328-OddEvenLinkedList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
// p: head of ll;
// r: ll;
// e:
// c1
// o
// /-------\
// 1 2 3 -> 4 -> 4
// \-------/
// e
//
//
var oddEvenList = function (head) {
if (!head) return head;
let odd = head;
let even = head.next;
let evenHead = head.next;
while (odd.next && even.next) {
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
};
// e:
// 1 2 1 2 1 2 1 2
// c1
// c1
// /-------\
// 2 1 3 -> 5 -> 6 -> 4 -> 7 -> 8
// \-------/
// c2
//
//
// var oddEvenList = function(head) {
// if (!head) return head;
// let c1 = head;
// let h2 = head.next;
// let c2 = head.next;
// while ((c1 && c1.next) || (c2 && c2.next)) {
// if (c1 && c1.next) {
// let n1 = c1.next.next;
// c1.next = c1.next.next;
// c1.next && (c1 = n1);
// }
// if (c2 && c2.next) {
// let n2 = c2.next.next;
// c2.next = c2.next.next;
// c2.next && (c2 = n2);
// }
// }
// c1.next = h2;
// return head;
// };