hello-algo/codes/csharp/utils/ListNode.cs
Yudong Jin 034ee65e9a
Fix bugs and harmonize the code comments (#1199)
* 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
2024-03-31 03:06:41 +08:00

32 lines
791 B
C#

// File: ListNode.cs
// Created Time: 2022-12-16
// Author: mingXta (1195669834@qq.com)
namespace hello_algo.utils;
/* 链表节点 */
public class ListNode(int x) {
public int val = x;
public ListNode? next;
/* 将数组反序列化为链表 */
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);
}
}