Esempio n. 1
0
 public ListNode RemoveNthFromEnd(ListNode head, int n)
 {
     var p = new ListNode(0);
     var tmp = new ListNode(0);
     p.next = head;
     tmp.next = head;
     while (--n >= 0)
     {
         p = p.next;
     }
     if (p.next == null)
     {
         head = head.next;
     }
     else
     {
         while (p.next != null)
         {
             p = p.next;
             tmp = tmp.next;
         }
         tmp = tmp.next;
     }
     return head;
 }
Esempio n. 2
0
        public ListNode MergeTwoLists(ListNode l1, ListNode l2)
        {
            if (l1 == null) return l2;
            if (l2 == null) return l1;
            ListNode p1 = new ListNode(0);
            ListNode p2 = new ListNode(0);
            ListNode prev = new ListNode(0);
            p1.next = l1;
            p2.next = l2;

            while (p2.next != null && p1.next.val >= p2.next.val)
            {
                prev.next = p2.next;
                p2.next = p2.next.next;
                prev.next.next = p1.next;
                l1 = prev.next;
            }
            while (p1.next != null && p2.next != null)
            {
                if (p1.next.val < p2.next.val)
                {
                    prev.next = p1.next;
                    p1.next = p1.next.next;
                }
                else
                {
                    prev.next.next = p2.next;
                    p2.next = p2.next.next;
                    prev.next.next.next = p1.next;
                }
            }
            if (p1.next == null)
            {
                prev.next = p2.next;
            }

            return l1;
        }