We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 870e071 commit 0a0ef4cCopy full SHA for 0a0ef4c
LeetCode/876. Middle of the Linked List/index.js
@@ -0,0 +1,30 @@
1
+/**
2
+ * Given the head of a singly linked list, return the middle node of the linked list
3
+ */
4
+
5
+function ListNode(val, next = null) {
6
+ this.val = (val === undefined ? 0 : val);
7
+ this.next = (next === undefined ? null : next);
8
+}
9
10
+var middleNode = function(head) {
11
+ let length = 0;
12
+ let current = head;
13
+ while (current !== null) {
14
+ length++;
15
+ current = current.next;
16
+ }
17
18
+ let middleIdx = Math.floor(length / 2);
19
+ current = head;
20
+ let currIdx = 0;
21
+ while (currIdx < middleIdx) {
22
+ currIdx++;
23
24
25
26
+ return current;
27
28
29
+const listNode = new ListNode(1);
30
+console.log(middleNode(listNode));
0 commit comments