mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-30 19:02:46 +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]>
37 lines
944 B
C#
37 lines
944 B
C#
// File: ListNode.cs
|
|
// Created Time: 2022-12-16
|
|
// Author: mingXta ([email protected])
|
|
|
|
namespace hello_algo.utils;
|
|
|
|
// [region]{list_node}
|
|
/* 链表节点 */
|
|
public class ListNode(int x) {
|
|
public int val = x; // 节点值
|
|
public ListNode? next; // 指向下一节点的引用
|
|
// [endregion]{list_node}
|
|
|
|
/* 将数组反序列化为链表 */
|
|
public static ListNode? ArrToLinkedList(int[] arr) {
|
|
ListNode dum = new(0);
|
|
ListNode head = dum;
|
|
foreach (int val in arr) {
|
|
head.next = new ListNode(val);
|
|
head = head.next;
|
|
}
|
|
return dum.next;
|
|
}
|
|
|
|
public override string? ToString() {
|
|
List<string> list = [];
|
|
var head = this;
|
|
while (head != null) {
|
|
list.Add(head.val.ToString());
|
|
head = head.next;
|
|
}
|
|
return string.Join("->", list);
|
|
}
|
|
// [region]{list_node_end}
|
|
}
|
|
// [endregion]{list_node_end}
|