mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 01:16:28 +08:00
56b20eff36
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
40 lines
1 KiB
C#
40 lines
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(int x) {
|
|
public int val = x;
|
|
public ListNode? next;
|
|
|
|
/* 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 = [];
|
|
var head = this;
|
|
while (head != null) {
|
|
list.Add(head.val.ToString());
|
|
head = head.next;
|
|
}
|
|
return string.Join("->", list);
|
|
}
|
|
}
|