You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
791 B
33 lines
791 B
// 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);
|
|
}
|
|
}
|