Exemplo n.º 1
0
        public static ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            bool     plusDecade = false;
            var      result     = new ListNode();
            ListNode iterator   = result;

            do
            {
                var sum = (l1?.val ?? 0) + (l2?.val ?? 0);

                if (plusDecade)
                {
                    sum += 1;
                }

                if (sum >= 10)
                {
                    sum       -= 10;
                    plusDecade = true;
                }
                else
                {
                    plusDecade = false;
                }

                if (result.next == null)
                {
                    result.val  = sum;
                    result.next = new ListNode();
                }
                else
                {
                    iterator.next = new ListNode(sum);
                    iterator      = iterator.next;
                }
                l1 = l1?.next;
                l2 = l2?.next;
            }while (l1 != null || l2 != null);

            return(result);
        }
Exemplo n.º 2
0
        public static ListNode RemoveElements(ListNode head, int val)
        {
            GetHead(head, val);

            return(head);
        }
Exemplo n.º 3
0
 public ListNode(int val = 0, ListNode next = null)
 {
     this.val  = val;
     this.next = next;
 }