mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 12:46:31 +08:00
034ee65e9a
* Fix the comment in array_deque.go * Fix the comment in bucket_sort.c * Translate the Java code comments to Chinese * Bug fixes * 二分查找 -> 二分搜尋 * Harmonize comments in `utils` between multiple programming languages
42 lines
795 B
C++
42 lines
795 B
C++
/**
|
|
* File: list_node.hpp
|
|
* Created Time: 2021-12-19
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
#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;
|
|
}
|
|
}
|