Exemple #1
0
        public ListNode MergeTwoLists(ListNode l1, ListNode l2)
        {
            if (l1 == null && l2 == null)
            {
                return(null);
            }

            if (l1 == null)
            {
                return(l2);
            }

            if (l2 == null)
            {
                return(l1);
            }

            ListNode curr   = new ListNode(0);
            ListNode result = curr;

            while (l1 != null && l2 != null)
            {
                if (l1.val <= l2.val)
                {
                    curr.next = l1;
                    l1        = l1.next;
                }
                else
                {
                    curr.next = l2;
                    l2        = l2.next;
                }

                curr = curr.next;
            }

            curr.next = l1 != null ? l1 : l2;

            return(result.next);
        }
 public LinkedList()
 {
     head  = null;
     count = 0;
 }
 public ListNode(int val = 0, ListNode next = null)
 {
     this.val  = val;
     this.next = next;
 }