Delete the Middle Node of a Linked List LeetCode solution in C++

1
**Intution * *
2
-we know that we can find middle node using two pointer fast and slow
3
.After we iterate through linkedlist slow pointer is our middle node and since we need
4
to delete it we only need pointer to its previous node and then just simply put next to
5
next node in previous node.Woahh !you are done with deleting middle NODE;
6

7
class Solution {
8
public:
9
ListNode *deleteMiddle(ListNode *head) {
10
if (head->next == nullptr) return nullptr;
11
ListNode *f = head;
12
ListNode *s = head, *prev;
13
while (f != nullptr && f->next != nullptr) {
14
f = f->next->next;
15
prev = s;
16
s = s->next;
17
}
18
prev->next = prev->next->next;
19
return head;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0