-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1001_linkedlist.cpp
executable file
·55 lines (43 loc) · 995 Bytes
/
1001_linkedlist.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
#include <iostream>
class Node {
public:
int data;
Node * next;
Node(int val, Node * nextNode): data(val), next(nextNode)
{
}
};
Node * head;
int main()
{
int N, tallest, shortest, count = 0;
std::cin>>N;
int val;
std::cin>>val;
head = new Node(val, nullptr);
for (int i = 1; i < N; i++)
{
std::cin>>val;
head->next = new Node(val, head->next);
std::swap(head->data, head->next->data);
Node * ptr = head->next->next;
tallest = head->next->data;
count++;
while (ptr)
{
shortest = std::min(head->data, ptr->data);
if (shortest >= tallest)
{
count++;
}
else if (head->data < tallest)
{
break;
}
tallest = std::max(tallest, ptr->data);
ptr = ptr->next;
}
}
std::cout<<count<<std::endl;
return 0;
}