int[]または文字列からListNodeを作成するクラス

文字列でもint[]でも構築可能

    public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode(int x) { val = x; }
    }
    class ListNodeHelper
    {
        public static ListNode CreateListNode(string nums)
        {
            if (nums == null)
                return null;
            string[] tmp = nums.Split("->");
            List<int> res = new List<int>();
            foreach (var item in tmp)
            {
                int number;
                if (int.TryParse(item, out number))
                    res.Add(number);
            }

            return CreateTree(res.ToArray());
        }
        public static ListNode CreateList(int[] nums)
        {
            if (nums == null)
                return null;
            ListNode res = new ListNode(-1);
            ListNode wk = res;
            for (int i = 0; i < nums.Length; i++)
            {
                wk.next = new ListNode(nums[i]);
                wk = wk.next;
            }
            return res.next;
        }
        public static string ResultStr(ListNode node)
        {
            StringBuilder builder = new StringBuilder();
            while (node != null)
            {
                builder.Append(node.val).Append("-");
                node = node.next;
            }
            return builder.ToString();
        }
    }

使い方(文字列で値を渡して区切り文字を指定Version。数値の配列でも可)

            ListNode res = ListNodeHelper.CreateListNode("1->2->3->4->5->NULL", "->");
            ListNode node = ListNodeHelper.CreateList(new int[] { 1, 2, 3, 4, 5 });
            Console.WriteLine(ListNodeHelper.ResultStr(res));