1function hasCycle(head) {
2 let slow = head, fast = head;
3 while (fast && fast.next) {
4 slow = slow.next;
5 fast = fast.next.next;
6 if (slow === fast) return true;
7 }
8 return false;
9}
10function detectCycle(head) {
11 let slow = head, fast = head;
12 while (fast && fast.next) {
13 slow = slow.next; fast = fast.next.next;
14 if (slow === fast) {
15 let p = head;
16 while (p !== slow) { p = p.next; slow = slow.next; }
17 return p;
18 }
19 }
20 return null;
21}
22function mergeTwoLists(a, b) {
23 const dummy = new ListNode(0);
24 let cur = dummy;
25 while (a && b) {
26 if (a.val <= b.val) { cur.next = a; a = a.next; }
27 else { cur.next = b; b = b.next; }
28 cur = cur.next;
29 }
30 cur.next = a || b;
31 return dummy.next;
32}
33function removeNthFromEnd(head, n) {
34 const dummy = new ListNode(0, head);
35 let fast = dummy, slow = dummy;
36 for (let i = 0; i < n; i++) fast = fast.next;
37 while (fast.next) { fast = fast.next; slow = slow.next; }
38 slow.next = slow.next.next;
39 return dummy.next;
40}