snippets 0.1.0
Loading...
Searching...
No Matches
linked_list.hpp
Go to the documentation of this file.
1#ifndef LINKED_LIST_H
2#define LINKED_LIST_H
3
4#include <vector>
5
6namespace hsc_snippets {
7 struct ListNode {
8 int val;
10
11 ListNode() : val(0), next(nullptr) {
12 }
13
14 explicit ListNode(int x) : val(x), next(nullptr) {
15 }
16
17 ListNode(int x, ListNode *next) : val(x), next(next) {
18 }
19 };
20
27 static int get_linked_list_length(ListNode *head) {
28 int len = 0;
29 while (head != nullptr) {
30 len += 1;
31 head = head->next;
32 }
33 return len;
34 }
35
42 static ListNode *make_linked_list(const std::vector<int> &v) {
43 if (v.empty()) {
44 return nullptr;
45 }
46 auto head = new ListNode;
47 ListNode *prev = nullptr;
48 auto it = v.begin();
49 while (it != v.end()) {
50 if (prev == nullptr) {
51 head->val = *it;
52 prev = head;
53 } else {
54 prev->next = new ListNode(*it);
55 prev = prev->next;
56 }
57 it = std::next(it);
58 }
59
60 return head;
61 }
62
69 static std::vector<int> linked_list_to_vector(ListNode *head) {
70 auto v = std::vector<int>{};
71 while (head != nullptr) {
72 v.push_back(head->val);
73 head = head->next;
74 }
75 return v;
76 }
77
83 static void linked_list_delete(ListNode *head) {
84 if (head == nullptr) {
85 return;
86 }
87 auto next = head->next;
88 while (next != nullptr) {
89 delete head;
90 head = next;
91 next = head->next;
92 }
93 delete head;
94 }
95
104 static ListNode *linked_list_remove(ListNode *head, size_t begin, size_t end) {
105 if (head == nullptr || begin >= end) {
106 // Nothing to remove
107 return head;
108 }
109
110 ListNode dummy(0, head); // Create a dummy node to simplify edge cases
111 ListNode *prev = &dummy;
112
113 // Move `prev` to the node just before `begin`
114 for (size_t i = 0; i < begin && prev->next != nullptr; ++i) {
115 prev = prev->next;
116 }
117
118 ListNode *current = prev->next;
119
120 // Remove nodes in range [begin, end)
121 for (size_t i = begin; i < end && current != nullptr; ++i) {
122 ListNode *temp = current;
123 current = current->next;
124 delete temp; // Free the removed node
125 }
126
127 // Connect the node before `begin` to the node at `end`
128 prev->next = current;
129
130 return dummy.next; // Return the new head, which is the next of dummy node
131 }
132
133
141 static ListNode *get_linked_list_ith_node(ListNode *head, size_t i) {
142 size_t current_index = 0;
143 while (head != nullptr) {
144 if (current_index == i) {
145 return head;
146 }
147 head = head->next;
148 current_index++;
149 }
150 return nullptr; // Index out of bounds
151 }
152}
153
154#endif // LINKED_LIST_H
Definition big_integer.hpp:14
static void linked_list_delete(ListNode *head)
Definition linked_list.hpp:83
static ListNode * linked_list_remove(ListNode *head, size_t begin, size_t end)
Definition linked_list.hpp:104
static ListNode * make_linked_list(const std::vector< int > &v)
Definition linked_list.hpp:42
static int get_linked_list_length(ListNode *head)
Definition linked_list.hpp:27
static ListNode * get_linked_list_ith_node(ListNode *head, size_t i)
Definition linked_list.hpp:141
static std::vector< int > linked_list_to_vector(ListNode *head)
Definition linked_list.hpp:69
Definition linked_list.hpp:7
ListNode(int x, ListNode *next)
Definition linked_list.hpp:17
int val
Definition linked_list.hpp:8
ListNode * next
Definition linked_list.hpp:9
ListNode(int x)
Definition linked_list.hpp:14
ListNode()
Definition linked_list.hpp:11