delete-the-middle-node-of-a-linked-list 1.0.0
Delete the Middle Node of a Linked List
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <bits/stdc++.h>
2
3using namespace std;
4
8struct ListNode
9{
10 int val;
12 ListNode() : val(0), next(nullptr) {}
13 ListNode(int x) : val(x), next(nullptr) {}
14 ListNode(int x, ListNode *next) : val(x), next(next) {}
15};
16
18{
19public:
21 {
22 if (head == NULL || head->next == NULL) // 0-el, 1-el
23 return NULL;
24
25 ListNode* slow = head;
26 ListNode* fast = head;
27 ListNode* prev = NULL;
28
29 while (fast != NULL && fast->next != NULL)
30 {
31 fast = fast->next->next;
32 prev = slow;
33 slow = slow->next;
34 }
35
36 prev->next = slow->next;
37 delete slow;
38
39 return head;
40 }
41};
42
43int main()
44{
45
46 return 0;
47}
ListNode * deleteMiddle(ListNode *head)
Definition main.cpp:20
int main()
Definition main.cpp:43
Definition for singly-linked list.
Definition main.cpp:9
int val
Definition main.cpp:10
ListNode(int x)
Definition main.cpp:13
ListNode()
Definition main.cpp:12
ListNode * next
Definition main.cpp:11
ListNode(int x, ListNode *next)
Definition main.cpp:14