-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.hpp
58 lines (51 loc) · 1.15 KB
/
list.hpp
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
//
// Created by Tomáš Petříček on 17.07.2023.
//
#ifndef LEET_CODE_LIST_HPP
#define LEET_CODE_LIST_HPP
#include <vector>
namespace list {
struct node {
int val;
node* next;
node()
:val(0), next(nullptr) { }
node(int x)
:val(x), next(nullptr) { }
node(int x, node* next)
:val(x), next(next) { }
};
node* create(const std::vector<int>& nums)
{
node ahead;
node* curr{&ahead};
for (auto num: nums) {
curr->next = new node(num);
curr = curr->next;
}
return ahead.next;
}
bool same(node* fst, node* snd)
{
while (fst && snd) {
if (fst->val!=snd->val) {
return false;
}
fst = fst->next;
snd = snd->next;
}
return !fst && !snd;
}
node* reverse(node* head)
{
node* curr, * next{nullptr};
while(head) {
curr = head;
head = head->next;
curr->next = next;
next = curr;
}
return next;
}
}
#endif //LEET_CODE_LIST_HPP