Files
hello-algo/ja/codes/cpp/utils/list_node.hpp
T
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

43 lines
879 B
C++

/**
* File: list_node.hpp
* Created Time: 2021-12-19
* Author: krahets ([email protected])
*/
#pragma once
#include <iostream>
#include <vector>
using namespace std;
/* 連結リストのノード */
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {
}
};
/* リストをデシリアライズして連結リストにする */
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;
}
/* 連結リストに確保したメモリ領域を解放する */
void freeMemoryLinkedList(ListNode *cur) {
// メモリを解放する
ListNode *pre;
while (cur != nullptr) {
pre = cur;
cur = cur->next;
delete pre;
}
}