2. Lituo title_adding two numbers

Add two numbers

You are given two non-empty linked lists representing two non-negative integers. Each of their digits is stored in reverse order, and each node can only store one digit.

Please add two numbers and return a linked list representing the sum in the same form.

You can assume that neither number starts with a zero other than the number zero.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    
    
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
    
    
     
            List<int> nodeList1 = new List<int>();
            List<int> nodeList2 = new List<int>();

            bool isEnd = false;
            ListNode newnode = new ListNode();
            newnode = l1;

            while (!isEnd)
            {
    
    
                nodeList1.Add(newnode.val);
                if (newnode.next == null)
                {
    
    
                    isEnd = true;
                }
                else
                {
    
    
                    newnode = newnode.next;
                }
            }


            isEnd = false;
            newnode = l2;
            while (!isEnd)
            {
    
    
                nodeList2.Add(newnode.val);
                if (newnode.next == null)
                {
    
    
                    isEnd = true;
                }
                else
                {
    
    
                    newnode = newnode.next;
                }
            }



            nodeList1.Reverse();
            nodeList2.Reverse();
            string list1String = "";
            string list2String = "";
            for (int i = 0; i < nodeList1.Count; i++)
            {
    
    
                list1String = list1String + nodeList1[i].ToString();
            }

            for (int i = 0; i < nodeList2.Count; i++)
            {
    
    
                list2String = list2String + nodeList2[i].ToString();
            }
            string stringCount = "";
            {
    
    

                if (list1String.Length > list2String.Length)
                {
    
    
                    string badd = list2String;
                    int ablength = list1String.Length - list2String.Length;
                    for (int i = 1; i <= ablength; i++)
                    {
    
    
                        badd = "0" + badd;
                    }
                    list2String = badd;
                }
                else if (list1String.Length < list2String.Length)
                {
    
    
                    string aadd = list1String;
                    int ablength = list2String.Length - list1String.Length;
                    for (int i = 1; i <= ablength; i++)
                    {
    
    
                        aadd = "0" + aadd;
                    }
                    list1String = aadd;
                }



                string abaddStr = "";
                int jinwei = 0;
                for (int i = list1String.Length - 1; i >= 0; i--)
                {
    
    
                    Console.WriteLine(i);
                    int abadd = int.Parse(list1String[i].ToString()) + int.Parse(list2String[i].ToString()) + jinwei;
                    jinwei = 0;

                    if (abadd >= 10)
                    {
    
    
                        jinwei = 1;
                        abadd = abadd - 10;
                        abaddStr = abadd.ToString() + abaddStr;
                    }
                    else
                    {
    
    
                        abaddStr = abadd + abaddStr;
                    }
                }
                if (jinwei == 1)
                {
    
    
                    abaddStr = "1" + abaddStr;
                }
                stringCount = abaddStr;

            }



            List<int> intList = new List<int>();
            for (int i = 0; i < stringCount.Length; i++)
            {
    
    
                string charstr = stringCount[i].ToString();
                intList.Add(int.Parse(charstr));
            }

            intList.Reverse();

            ListNode sentineNode = new ListNode(0);  //建立哨兵节点     //开始节点
            ListNode tempNode = sentineNode;  //tempNode变量指向哨兵节点    //临时节点1
            for (int i = 0; i < intList.Count; i++)
            {
    
    
                ListNode newNode = new ListNode(intList[i]);
                tempNode.next = newNode; //临时节点‘后继指针’指向新节点
                tempNode = newNode; //把新节点赋给临时变量tempNode
            }

            ListNode sentineNode1 = new ListNode(0);
            sentineNode1 = sentineNode.next;

            return sentineNode1;
                  
    }
}



Guess you like

Origin blog.csdn.net/GoodCooking/article/details/130650386