链表的类通常表示如下:

public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode(int val, ListNode next=null)
        {
            this.val = val;
            this.next = next;
        }
    }

(一)创建链表

根据给定的数组使用尾插法和头插法创建链表

public static ListNode createListNode(int[] array)
        {
            ListNode node = new ListNode(array[0]);
            for (int i = 1; i < array.Length; i++)
            {
                addNode(node, array[i]);
            }
            return node;
        }

        private static void addNode(ListNode node, int val)
        {
            if (node.next == null)
                node.next = new ListNode(val);
            else
                addNode(node.next, val);
        }
尾插法

相关文章:

  • 2021-09-07
  • 2019-08-13
  • 2022-02-17
  • 2022-01-05
  • 2021-12-18
  • 2022-12-23
  • 2021-12-31
  • 2021-09-02
猜你喜欢
  • 2021-06-03
  • 2021-12-31
  • 2021-04-28
  • 2022-12-23
  • 2021-11-16
  • 2021-08-06
  • 2022-12-23
相关资源
相似解决方案