zoukankan      html  css  js  c++  java
  • leetcode Add Two Numbers c#版本

    You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

    You may assume the two numbers do not contain any leading zero, except the number 0 itself.

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8

    话不多说,直接上代码

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace ConsoleApplication2
      8 {
      9      public class ListNode {
     10      public int val;
     11      public ListNode next;
     12      public ListNode(int x) { val = x; }
     13  }
     14 
     15 
     16     public class Solution
     17     {
     18         public Solution()
     19         {
     20         }
     21 
     22         public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
     23         {
     24             ListNode rev1 = ReverseList(l1);
     25             ListNode rev2 = ReverseList(l2);
     26             ListNode head, last;
     27             ListNode param1, param2;
     28             param1 = rev1;
     29             param2 = rev2;
     30             head = last = null;
     31             while (param1 != null || param2 != null)
     32             {
     33                 ListNode cur = new ListNode(0);
     34                 if (param1 != null)
     35                 {
     36                     cur.val += param1.val;
     37                     param1 = param1.next;
     38                 }
     39                 if (param2 != null)
     40                 {
     41                     cur.val += param2.val;
     42                     param2 = param2.next;
     43                 }
     44                 if (last == null)
     45                 {
     46                     last = head = cur;
     47                 }
     48                 else
     49                 {
     50                     last.next = cur;
     51                     last = cur;
     52                 }
     53             }
     54             ListNode ret = ReverseList(head);
     55             return ret;
     56         }
     57         ListNode ReverseList(ListNode ori)
     58         {
     59             ListNode cur, next, afterNext, ret;
     60             ret = null;
     61             cur = ori;
     62             next = cur.next;
     63             afterNext = null;
     64             while (true)
     65             {
     66                 if (GetNextTwo(cur, ref next, ref afterNext))
     67                 {
     68                     next.next = cur;
     69                     cur = next;
     70 
     71                     next = afterNext;
     72                     afterNext = next.next;
     73 
     74                 }
     75                 else
     76                 {
     77                     if (next == null)
     78                     {
     79                         break;
     80                     }
     81                     else if (afterNext == null)
     82                     {
     83                         next.next = cur;
     84                         ret = next;
     85                         break;
     86                     }
     87                 }
     88             }
     89             ori.next = null;
     90             return ret;
     91         }
     92         bool GetNextTwo(ListNode head, ref ListNode next, ref ListNode afterNext)
     93         {
     94             bool haveNextTwo = false;
     95             if (null == next && null != head.next)
     96             {
     97                 next = head.next;
     98             }
     99             if (null != next.next)
    100             {
    101                 afterNext = next.next;
    102                 haveNextTwo = true;
    103             }
    104             return haveNextTwo;
    105         }
    106     }
    107 
    108 }
  • 相关阅读:
    天兔监控系统安装
    day6
    day5
    day4
    day3
    day2
    day1
    几个重要的Xenomai相关链接
    树莓派GPIO中断驱动程序
    转了一圈,再读LKD
  • 原文地址:https://www.cnblogs.com/lpcoder/p/6797097.html
Copyright © 2011-2022 走看看