forked from BitsPleaseMSI/BitsPlease-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.cpp
60 lines (50 loc) · 994 Bytes
/
8.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int data;
struct ListNode* next;
};
struct ListNode* NewListNode(int data)
{
struct ListNode* node = new ListNode;
node->data = data;
node->next = NULL;
return node;
}
void PrintList(struct ListNode* head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head = head->next;
}
}
struct ListNode* RemoveMiddleNode(struct ListNode* head)
{
ListNode *prev, *slow, *fast;
slow=fast=head;
while(fast && fast->next!=NULL) {
prev=slow;
slow=slow->next;
fast=fast->next->next;
}
prev->next=prev->next->next;
delete slow;
return head;
}
int main()
{
struct ListNode* head = NewListNode(10);
struct ListNode* newhead = head;
for(int i=10;i<=24;i+=2)
{
newhead->next = NewListNode(i);
newhead = newhead->next;
}
PrintList(head);
newhead = RemoveMiddleNode(head);
printf("\n");
PrintList(newhead);
return 0;
}