C# ListNodeHelper

  • 30
  • 0
  • 2020-11-23

為了方便測試 ListNode (Linked list)建立的,記錄一下

Node Class(泛型):

public class ListNode<T>
{
	public T val;
	public ListNode<T> next;
	public ListNode(T val, ListNode<T> next = null)
	{
		this.val = val;
		this.next = next;
	}
}

Helper Class:

public static class ListNodeHelper
{
	public static IEnumerable<T> Enumerate<T>(this ListNode<T> lst)
	{
		while (lst != null)
		{
			yield return lst.val;
			lst = lst.next;
		}
	}

	public static ListNode<T> Build<T>(params T[] lst)
	{
		ListNode<T> head = null, last = null;
		foreach (var item in lst)
		{
			var node = new ListNode<T>(item);
			if (head == null)
				head = node;
			if (last != null)
				last.next = node;
			last = node;
		}
		return head;
	}

}