mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Add the initial EN translation for C++ code (#1346)
This commit is contained in:
42
en/codes/cpp/utils/list_node.hpp
Normal file
42
en/codes/cpp/utils/list_node.hpp
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* File: list_node.hpp
|
||||
* Created Time: 2021-12-19
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* Linked list node */
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(nullptr) {
|
||||
}
|
||||
};
|
||||
|
||||
/* Deserialize a list into a linked list */
|
||||
ListNode *vecToLinkedList(vector<int> list) {
|
||||
ListNode *dum = new ListNode(0);
|
||||
ListNode *head = dum;
|
||||
for (int val : list) {
|
||||
head->next = new ListNode(val);
|
||||
head = head->next;
|
||||
}
|
||||
return dum->next;
|
||||
}
|
||||
|
||||
/* Free memory allocated to the linked list */
|
||||
void freeMemoryLinkedList(ListNode *cur) {
|
||||
// Free memory
|
||||
ListNode *pre;
|
||||
while (cur != nullptr) {
|
||||
pre = cur;
|
||||
cur = cur->next;
|
||||
delete pre;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user