-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.cpp
More file actions
50 lines (45 loc) · 1.13 KB
/
Copy pathlink.cpp
File metadata and controls
50 lines (45 loc) · 1.13 KB
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
#include "link.h"
void LinkList::createLink(std::vector<int> &nums) {
ListNode *ptr = this->head;
for (int num : nums) {
this->size++;
ptr->next = new ListNode(num);
ptr = ptr->next;
}
}
void LinkList::deleteLink(void) {
ListNode *ptr = this->head->next, *tmp;
while (ptr != nullptr) {
tmp = ptr;
ptr = ptr->next;
tmp = nullptr;
this->size--;
delete tmp;
}
}
ListNode *LinkList::getNode(int index) {
if (index < 0) index = this->size + index;
if (index < 0 || index >= this->size) return nullptr;
ListNode *ptr = this->head->next;
for (int i = 0; i < index; ++i) ptr = ptr->next;
return ptr;
}
std::vector<int> LinkList::toVector(void) {
this->resetSize();
std::vector<int> nums(this->size);
ListNode *ptr = head->next;
for (int i = 0; i < this->size; ++i) {
nums[i] = ptr->val;
ptr = ptr->next;
}
return nums;
}
void LinkList::resetSize(void) {
int size = 0;
ListNode *ptr = this->head->next;
while (ptr != nullptr) {
size++;
ptr = ptr->next;
}
this->size = size;
}