Files
hello-algo/ru/codes/cpp/utils/list_node.hpp
T
Yudong JinandClaude Opus 5.5 ded7ddc635 Revert the runnable-snippets batches to the #2033 state (#2038)
Reverts #2034, #2035, #2036 and #2037. The work will return in a single
reviewed PR that is completed before it merges. The tree equals 8c96e2f30
(#2033).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-16 06:45:32 +08:00

43 lines
940 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;
}
}