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
3
using namespace
std;
4
8
struct
ListNode
9
{
10
int
val
;
11
ListNode
*
next
;
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
17
class
Solution
18
{
19
public
:
20
ListNode
*
deleteMiddle
(
ListNode
* head)
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
43
int
main
()
44
{
45
46
return
0;
47
}
Solution
Definition
main.cpp:18
Solution::deleteMiddle
ListNode * deleteMiddle(ListNode *head)
Definition
main.cpp:20
main
int main()
Definition
main.cpp:43
ListNode
Definition for singly-linked list.
Definition
main.cpp:9
ListNode::val
int val
Definition
main.cpp:10
ListNode::ListNode
ListNode(int x)
Definition
main.cpp:13
ListNode::ListNode
ListNode()
Definition
main.cpp:12
ListNode::next
ListNode * next
Definition
main.cpp:11
ListNode::ListNode
ListNode(int x, ListNode *next)
Definition
main.cpp:14
main.cpp
Generated by
1.9.8