We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 71fda8b commit a3eda4fCopy full SHA for a3eda4f
2095-delete-the-middle-node-of-a-linked-list.js
@@ -0,0 +1,36 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
9
+ * @param {ListNode} head
10
+ * @return {ListNode}
11
12
+const deleteMiddle = function(head) {
13
+ if(head == null) return head
14
+ const dummy = new ListNode(null, head)
15
+ let n = 0, cur = head
16
+ while(cur) {
17
+ n++
18
+ cur = cur.next
19
+ }
20
+ if(n === 1) return null
21
+ const mid = Math.floor(n / 2)
22
+ cur = dummy.next
23
+ let pre = dummy
24
+ for(let i = 0; i < n; i++) {
25
+ if(i === mid - 1) {
26
+ pre = cur
27
+ // pre.next = cur.next.next
28
29
+ if(i === mid) {
30
+ pre.next = cur.next
31
32
+ if(i > mid) break
33
34
35
+ return dummy.next
36
+};
0 commit comments