mirror of
https://github.com/krahets/hello-algo.git
synced 2026-12-01 19:31:00 +08:00
Batch 3a of the runnable-snippets plan. heap_operations now cites the heap demos in Python, C++, Java, C#, Go, Swift, Rust and Kotlin; the demos call the library directly instead of push/pop helpers and print each popped value. list_node_class cites each language's shared ListNode (Swift stays literal) and doubly_list_node_class cites linkedlist_deque (Go stays literal). Node comments adopt reviewed source wording, Rust and Swift get import regions, the Ruby typo 前躯 becomes 前驱, and the TypeScript doubly node fields become nullable. en, ja, ko and zh-hant are regenerated; ru is synced by hand. Co-Authored-By: Claude Opus 5.5 <[email protected]>
45 lines
911 B
C++
45 lines
911 B
C++
/**
|
|
* File: list_node.hpp
|
|
* Created Time: 2021-12-19
|
|
* Author: krahets ([email protected])
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
// [region]{list_node}
|
|
/* Linked list node */
|
|
struct ListNode {
|
|
int val; // Node value
|
|
ListNode *next; // Pointer to the next node
|
|
ListNode(int x) : val(x), next(nullptr) {
|
|
}
|
|
};
|
|
// [endregion]{list_node}
|
|
|
|
/* 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 the memory allocated to the linked list */
|
|
void freeMemoryLinkedList(ListNode *cur) {
|
|
// Free memory
|
|
ListNode *pre;
|
|
while (cur != nullptr) {
|
|
pre = cur;
|
|
cur = cur->next;
|
|
delete pre;
|
|
}
|
|
}
|