Skip to content

Commit 7de8bc8

Browse files
committed
😢 [143] Online solution
1 parent 581e476 commit 7de8bc8

File tree

3 files changed

+192
-79
lines changed

3 files changed

+192
-79
lines changed

141/solution.js

+36-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
/**
22
* Definition for singly-linked list.
3-
* function ListNode(val) {
4-
* this.val = val;
5-
* this.next = null;
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
66
* }
77
*/
8+
class ListNode {
9+
constructor(val, next) {
10+
this.val = (val === undefined ? 0 : val);
11+
this.next = (next === undefined ? null : next);
12+
}
13+
}
814

915
/**
1016
* @param {ListNode} head
11-
* @return {boolean}
17+
* @return {void} Do not return anything, modify head in-place instead.
1218
*/
13-
const hasCycle = (head) => { // O(n), O(1)
14-
let slow = head, fast = head;
19+
const reorderList = (head) => {
20+
let slow = head, fast = head.next;
1521

16-
while (null !== fast && null !== fast.next) {
22+
while (fast && fast.next) {
1723
slow = slow.next;
1824
fast = fast.next.next;
19-
if (slow == fast) return true;
2025
}
2126

22-
return false;
23-
}
27+
let secondHalf = slow.next;
28+
slow.next = null; // This is the magical part that breaks the links
29+
30+
let prev = null
31+
while (secondHalf) {
32+
let tmp = secondHalf.next;
33+
secondHalf.next = prev;
34+
prev = secondHalf;
35+
secondHalf = tmp;
36+
}
37+
let part2 = prev, part1 = head;
38+
39+
while (part2) {
40+
let tmp1 = part1.next,
41+
tmp2 = part2.next;
42+
43+
part1.next = part2; // Link to node of reversed part
44+
part2.next = tmp1; // Link to the tmp of the next of part1
45+
46+
part1 = tmp1;
47+
part2 = tmp2;
48+
}
49+
};

143/chatgpt_solution.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class ListNode {
2+
constructor(val, next = null) {
3+
this.val = val;
4+
this.next = next;
5+
}
6+
}
7+
8+
function reorderList(head) {
9+
if (!head || !head.next) {
10+
return head;
11+
}
12+
13+
// Step 1: Find the middle of the linked list
14+
let slow = head;
15+
let fast = head.next;
16+
while (fast && fast.next) {
17+
slow = slow.next;
18+
fast = fast.next.next;
19+
}
20+
21+
// Step 2: Split the list into two halves
22+
let secondHalf = slow.next;
23+
slow.next = null;
24+
25+
// Step 3: Reverse the second half of the linked list
26+
let prev = null;
27+
let current = secondHalf;
28+
while (current) {
29+
let nextNode = current.next;
30+
current.next = prev;
31+
prev = current;
32+
current = nextNode;
33+
}
34+
secondHalf = prev;
35+
36+
// Step 4: Merge the first half and reversed second half alternately
37+
let current1 = head;
38+
let current2 = secondHalf;
39+
while (current2) {
40+
let nextNode1 = current1.next;
41+
let nextNode2 = current2.next;
42+
43+
current1.next = current2;
44+
current2.next = nextNode1;
45+
46+
current1 = nextNode1;
47+
current2 = nextNode2;
48+
}
49+
50+
return head;
51+
}

143/my_solution.js

+105-69
Original file line numberDiff line numberDiff line change
@@ -19,99 +19,135 @@ class ListNode {
1919
* @return {void} Do not return anything, modify head in-place instead.
2020
*/
2121
const reorderList = (head) => {
22-
// let slow = head, fast = head.next;//, reversed = new ListNode(), dummy = new ListNode(), result = dummy;
23-
// while (fast && fast.next) {
24-
// slow = slow.next
25-
// fast = fast.next.next;
26-
// }
22+
let slow = head, fast = head.next;
23+
24+
while (fast && fast.next) {
25+
slow = slow.next;
26+
fast = fast.next.next;
27+
}
28+
29+
/*
30+
31+
In the provided code, the original implementation splits the linked list into two halves by setting the slow.next pointer to null. This is done to separate the first half (head to slow) from the second half (slow.next to the end).
32+
33+
The reason for setting slow.next = null is to disconnect the two halves so that the second half can be reversed independently. This ensures that the first half doesn't have any reference to the reversed second half. It simplifies the merging step later.
34+
35+
In your suggested code, you're attempting to reverse the second half while keeping the reference to the first half intact. While it's possible to reverse the second half this way, it would make the merging step more complex since you'd need to traverse both the first and reversed second halves simultaneously to interleave the nodes.
36+
37+
*/
38+
// // MY REVERSE: -- DOES NOT WORK
39+
// slow = slow.next; // Shift from [2,3,4] -> [3,4] in order to reverse as full LL -> [1,2,3,4]
40+
// console.log("-> slow")
41+
// console.log(slow)
2742
//
28-
// console.log("slow")
29-
// console.log(slow)
30-
// console.log("fast")
31-
// console.log(fast)
43+
// let part2 = new ListNode(); // reversed
44+
// while (slow) {
45+
// part2.next = new ListNode(slow.val, part2.next)
46+
// slow = slow.next;
47+
// }
48+
// part2 = part2.next;
49+
// // Reversed
50+
// console.log("---> Reversed -> part2")
51+
// console.log(part2)
52+
// console.log("_____________________________")
53+
// let part1 = head;
54+
55+
// ONLINE:
56+
let secondHalf = slow.next;
57+
slow.next = null; // This is the magical part that breaks the links
58+
// console.log("curr")
59+
// console.log(curr)
60+
61+
let prev = null
62+
while (secondHalf) {
63+
let tmp = secondHalf.next;
64+
secondHalf.next = prev;
65+
prev = secondHalf;
66+
secondHalf = tmp;
67+
}
68+
let part2 = prev;
3269

33-
let slow = head, fast = head, counter = head, reversed = new ListNode(), dummy = new ListNode(), result = dummy;
70+
// Reversed
71+
console.log("Reversed -> part2")
72+
console.log(part2)
73+
// console.log("prev")
74+
// console.log(prev)
75+
// let part2 = prev;
3476

35-
// console.log("fast")
36-
// console.log(fast)
77+
let part1 = head;
3778

38-
// let count = 0;
39-
// while (counter.next) {
40-
// count++;
41-
// counter = counter.next;
42-
// }
79+
console.log("-> part1")
80+
console.log(part1)
4381

44-
// while (count > 0) {
45-
// reversed.next = new ListNode(fast.val, reversed.next);
46-
// fast = fast.next;
82+
// console.log("part1 check")
83+
// while (part1) {
84+
// console.log(part1.val)
85+
// part1 = part1.next;
4786
// }
4887

49-
console.log("slow")
50-
console.log(slow)
51-
reversed = reversed.next;
52-
console.log("reversed")
53-
console.log(reversed)
54-
console.log("count: " + count)
55-
56-
while (slow || reversed) {
57-
// console.log(`-> slow:${slow.val} & reversed:${reversed.val}`)
58-
// when reversed is null (on case of 3 linked list only), or same values.
59-
if ((null === reversed) || (reversed && (slow.val === reversed.val))) {
60-
// if (slow.val === reversed.val) {
61-
console.log('------------------> FOUnd same')
62-
console.log(dummy)
63-
slow.next = null;
64-
dummy.next = slow;
65-
slow = null;
66-
reversed = null;
67-
} else {
68-
// dummy.next = new ListNode(slow.val, reversed);
69-
dummy.next = slow;
70-
slow = slow.next;
71-
dummy = dummy.next;
72-
73-
dummy.next = reversed;
74-
reversed = reversed.next;
75-
dummy = dummy.next;
76-
77-
console.log("---> dummy")
78-
console.log(dummy)
79-
}
88+
while (part2) {
89+
90+
// console.log("-> part2")
91+
// console.log(part2)
92+
93+
let tmp1 = part1.next,
94+
tmp2 = part2.next;
95+
96+
console.log("tmp1 & tmp2")
97+
console.log(tmp1)
98+
console.log(tmp2)
8099

100+
part1.next = part2; // Link to node of reversed part
101+
part2.next = tmp1; // Link to the tmp of the next of part1
102+
103+
part1 = tmp1;
104+
part2 = tmp2;
105+
console.log("ending ->")
106+
console.log(part1)
107+
console.log(part2)
81108
}
82-
result = result.next
83-
console.log("result")
84-
console.log(result)
85-
while (result) {
86-
console.log("check")
87-
console.log(result.val)
88-
result = result.next;
109+
110+
// console.log("part1 <-")
111+
// console.log(part1)
112+
// console.log("part2")
113+
// console.log(part2)
114+
115+
console.log("head check")
116+
while (head) {
117+
console.log(head.val)
118+
head = head.next;
89119
}
120+
90121
};
91122

92123

93124

125+
reorderList(
126+
new ListNode(1,
127+
new ListNode(2,
128+
new ListNode(3,
129+
new ListNode(4))
130+
)
131+
)
132+
)
133+
94134
// reorderList(
95135
// new ListNode(1,
96136
// new ListNode(2,
97-
// new ListNode(3,
98-
// new ListNode(4))
137+
// new ListNode(3)
99138
// )
100139
// )
101140
// )
102141

142+
103143
// reorderList(
104144
// new ListNode(1,
105145
// new ListNode(2,
106-
// new ListNode(3)
146+
// new ListNode(3,
147+
// new ListNode(4,
148+
// new ListNode(5)
149+
// )
150+
// )
107151
// )
108152
// )
109153
// )
110-
111-
112-
reorderList(
113-
new ListNode(1,
114-
new ListNode(2,
115-
new ListNode(3,
116-
new ListNode(4,
117-
new ListNode(5))))))

0 commit comments

Comments
 (0)