2. Add Two Numbers - LeetCode

Keywords: Java Swift

LeetCode Problems Solutions

question description: problem description

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.
Given two non-empty linked lists, two non-negative integers are represented. Numbers are stored in reverse (the previous node points to the next node) and each node contains a number. Add the numbers of the two linked lists and return them as a linked list.
You can assume that neither number is developed with 0 unless it is the zero number itself.

Simple understanding: is to add the number of the corresponding nodes of the two linked lists, take the remaining number greater than 10 and backward one bit, return a new linked list. (The list here is ListNode below, which can be understood as a class with its own properties)
Example:

/**
 * Definition for singly-linked list. //Define a single linked list
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; } //Construction method
 * }
 */

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

Thinking

Three points need to be clarified:
a. val value of each node is obtained by adding modulus (%) to the value of the corresponding nodes of the given two linked lists.
b. val value of each node is also affected by the sum of Vals of the previous node divided by 10 quotients, so in a, the quotient value of the corresponding nodes of the two linked lists is also needed after adding the Vals of the corresponding nodes of the previous node.
c. The number (value) of each node in the returned list is determined by the next value of the previous node, that is, the tail node of the list.
Therefore, we can first determine the first node of the final list, and then continue to set the next value of the list to determine the following nodes.

solution with java - Java solution

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

//First determine the first node
        ListNode result = new ListNode((l1.val + l2.val) % 10);
//Record the impact of the first node on the next node, whether to enter a bit or not
        int count = (l1.val + l2.val) / 10;
        int index = 0; // Records are set to the number of nodes

        ListNode node1 = l1.next;
        ListNode node2 = l2.next;

//The length of the two lists may be different, and the final next value stops the loop at the same time as space-time.
        while (node1 != null || node2 != null){

            index += 1;
           

            int arg1 = (node1 == null) ? 0 : node1.val;
            int arg2 = (node2 == null) ? 0 : node2.val;

            int val = (arg1 + arg2) % 10;
            int nextVal = (count + arg1 + arg2) / 10;

            ListNode temp = new ListNode((count + val) % 10);

            //Assign the last next of result to temp
            result = method(index-1,result,temp);


//Updating cyclic conditions
            count = nextVal;
            if (node1 != null){
                node1 = node1.next;
            }
            
            if (node2 != null){
                node2 = node2.next;
            }
            
        }
       
//When the length of the two linked lists is the same and the number of the last nodes is increased to 10, the new linked list length will have one more node, that is count = 1. It is no problem to use count > 0 directly here.
       if (count > 0){
           
           ListNode temp = new ListNode(count);
           
           result = method(index,result,temp);
       }

        return result;


    }
    
    // Recursive Solution to Modify next Value of Location Element in Link List

/*
index:Modify the next value of the number of nodes in the list, starting at 0
result:Target list to be modified
value:Modified value
*/

    private ListNode method(int index, ListNode result, ListNode value){

        if (index > 0){

            index -= 1;

            result.next = method(index, result.next,value);

        }else{
            result.next = value;
            return result;
        }

        return result;

    }

solution with swift - swift solution

func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
        //First determine the first node
        var result = ListNode(((l1?.val)! + (l2?.val)!) % 10);
        //Record the impact of the first node on the next node, whether to enter a bit or not
        var count = ((l1?.val)! + (l2?.val)!) / 10;
        var index = 0;// Records are set to the number of nodes
        
        var node1 = l1?.next;
        var node2 = l2?.next;
        //The length of the two lists may be different, and the final next value stops the loop at the same time as space-time.
        while (node1 != nil || node2 != nil){
            
            index += 1;
            
            
            let arg1 = (node1 == nil) ? 0 : node1?.val;
            let arg2 = (node2 == nil) ? 0 : node2?.val;
            
            let val = (arg1! + arg2!) % 10;
            let nextVal = (count + arg1! + arg2!) / 10;
            
            let temp = ListNode((count + val) % 10);
            
            //Assign the last next of result to temp
            result = method(index: index-1,result: result,value: temp);
            

//Updating cyclic conditions
            count = nextVal;
            if (node1 != nil){
                node1 = node1?.next;
            }
            if (node2 != nil){
                
                node2 = node2?.next;
            }
  
        }
        
        if (count > 0){
            
            let temp = ListNode(count);
            
            result = method(index: index,result: result,value: temp);
        }

        return result;
    
    }
    

    // Recursive Solution to Modify next Value of Location Element in Link List
/*
index:Modify the next value of the number of nodes in the list, starting at 0
result:Target list to be modified
value:Modified value
*/
    func method(index:Int, result : ListNode, value : ListNode)->ListNode{
    
        var i = index
        
        if (i > 0){
            
            i -= 1;
            
            result.next = method(index: i, result: result.next!,value: value);
            
        }else{
            result.next = value;
            return result;
        }
        
        return result;
        
    }

Posted by roxki on Wed, 09 Jan 2019 22:33:10 -0800