Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b77ac6b

Browse files
feat(LC): add 876, successful answer, optimized, notes
1 parent 9a1145b commit b77ac6b

File tree

3 files changed

+118
-5
lines changed

3 files changed

+118
-5
lines changed

‎LeetCode/###_leetcode.template.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
/**
1414
* CODE
15+
* TIME:
16+
* MEMORY:
1517
*/
1618

1719
/**
@@ -24,7 +26,7 @@
2426
* READ 0
2527
* EXPLAIN 0
2628
* APPROACH 0
27-
* CODE [FAIl] 0
29+
* CODE [FAIL] 0
2830
* TEST 0
2931
* OPTIMIZE 0
3032
*/

‎LeetCode/Completed/206_ReverseLinkedList/206_leetcode.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ var reverseList = function (currNode, prevNode) {};
4949
*/
5050

5151
/**
52-
* READ 0
53-
* EXPLAIN 0
54-
* APPROACH 0
55-
* CODE [FAIl] 0
52+
* READ 01:29
53+
* EXPLAIN 11:54 13:23
54+
* APPROACH 09:01 22:25
55+
* CODE [FAIl] 10:14 32:39
5656
* TEST 0
5757
* OPTIMIZE 0
5858
*/
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @see {@link https://leetcode.com/problems/middle-of-the-linked-list/?envType=study-plan&id=level-1 876. Middle of the Linked List}
3+
*/
4+
5+
/**
6+
* EXPLAIN
7+
*
8+
* given LL return
9+
* middle node if LL has ODD length
10+
* 2nd middle if LL has even length
11+
*
12+
* guaranteed to always have at least 1 node, at most 100
13+
* vals are between 1 and 100
14+
*/
15+
16+
/**
17+
* APPROACH
18+
*
19+
* returning a NODE
20+
* need a pointer to know what node to return, this is mid node
21+
* need a pointer to advance the LL node searcher
22+
* need a flag to know WHEN to advance mid node, defaults to true, then flips
23+
*
24+
*
25+
* START LOOP
26+
* IF no node.next
27+
* return mid node
28+
* ELSE (node.next DOES exists)
29+
* advance LL node searcher to next node
30+
*
31+
* IF flag to advance mid node is true
32+
* advance mid node pointer
33+
* set flag to false
34+
* ELSE
35+
* set flag to true to trigger mid advancement NEXT loop
36+
* if reaches this point
37+
* do nothing else
38+
* END LOOP
39+
*
40+
* curr endPointer is null, reached end of LL
41+
* return current mid node
42+
*/
43+
44+
/**
45+
* CODE
46+
* TIME: 75ms - 47%
47+
* MEMORY: 42.3MB - 25%
48+
*/
49+
/**
50+
* Definition for singly-linked list.
51+
* function ListNode(val, next) {
52+
* this.val = (val===undefined ? 0 : val)
53+
* this.next = (next===undefined ? null : next)
54+
* }
55+
*/
56+
/**
57+
* @param {ListNode} head
58+
* @return {ListNode}
59+
*/
60+
var middleNode = function (head) {
61+
let endNode = head;
62+
let midNode = head;
63+
let advanceMidNode = true;
64+
65+
while (endNode) {
66+
if (!endNode.next) {
67+
return midNode;
68+
}
69+
70+
endNode = endNode.next;
71+
72+
if (advanceMidNode) {
73+
midNode = midNode.next;
74+
}
75+
76+
advanceMidNode = !advanceMidNode;
77+
}
78+
79+
return midNode;
80+
};
81+
/**
82+
* OPTIMIZED
83+
* TIME: 66ms - 69%
84+
* MEMORY: 42.1MB - 49%
85+
* Uses a fast pointer that advances two at a time
86+
* and a slow pointer that advances one at a time
87+
*/
88+
89+
var middleNode = function (head) {
90+
let fast = head.next;
91+
while (fast) {
92+
head = head.next;
93+
fast = fast.next?.next;
94+
}
95+
return head;
96+
};
97+
98+
/**
99+
* COMPLEXITY
100+
* TIME O(n), where n is the length of the LL
101+
* SPACE O(1), only ever storing one node per variable at a time, not a growing value set
102+
*/
103+
104+
/**
105+
* READ 00:47
106+
* EXPLAIN 01:31 02:18
107+
* APPROACH 12:48 15:07
108+
* CODE [PASS] 12:27 27:36
109+
* TEST 06:01 33:37
110+
* OPTIMIZE 24:37 58:38
111+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /