-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
51 lines (39 loc) · 891 Bytes
/
solution.ts
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
/*
* @lc app=leetcode id=328 lang=javascript
*
* [328] Odd Even Linked List
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
type MaybeList = ListNode | null;
interface ListNode {
val: number;
next: MaybeList;
}
/**
* @param {ListNode} head
* @return {ListNode}
*/
const oddEvenList = (head: MaybeList): MaybeList => {
// * ['64 ms', '83.74 %', '36.2 MB', '100 %']
if (head === null) return head;
const dummy = { next: null } as ListNode;
let p1: ListNode = head;
let p2: MaybeList = dummy;
while (p1.next !== null && p1.next.next !== null) {
p2 = p2.next = p1.next;
p1 = p1.next = p1.next!.next;
}
if (p1.next !== null) p2 = p2.next = p1.next;
p2.next = null;
p1.next = dummy.next;
return head;
};
// @lc code=end
export { oddEvenList };