hello-algo/codes/csharp/utils/ListNode.cs
hpstory f62256bee1
fix(csharp): Modify method name to PascalCase, simplify new expression (#840)
* Modify method name to PascalCase(array and linked list)

* Modify method name to PascalCase(backtracking)

* Modify method name to PascalCase(computational complexity)

* Modify method name to PascalCase(divide and conquer)

* Modify method name to PascalCase(dynamic programming)

* Modify method name to PascalCase(graph)

* Modify method name to PascalCase(greedy)

* Modify method name to PascalCase(hashing)

* Modify method name to PascalCase(heap)

* Modify method name to PascalCase(searching)

* Modify method name to PascalCase(sorting)

* Modify method name to PascalCase(stack and queue)

* Modify method name to PascalCase(tree)

* local check
2023-10-07 12:33:46 -05:00

44 lines
1.1 KiB
C#

// File: ListNode.cs
// Created Time: 2022-12-16
// Author: mingXta (1195669834@qq.com)
namespace hello_algo.utils;
/* Definition for a singly-linked list node */
public class ListNode {
public int val;
public ListNode? next;
public ListNode(int x) {
val = x;
}
/* Generate a linked list with an array */
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;
}
/* Get a list node with specific value from a linked list */
public static ListNode? GetListNode(ListNode? head, int val) {
while (head != null && head.val != val) {
head = head.next;
}
return head;
}
public override string? ToString() {
List<string> list = new();
var head = this;
while (head != null) {
list.Add(head.val.ToString());
head = head.next;
}
return string.Join("->", list);
}
}