2. 力扣题目_两数字相加

两数字相加

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

/**
 * 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;
                  
    }
}



猜你喜欢

转载自blog.csdn.net/GoodCooking/article/details/130650386