链表相加两数:LeetCode

huangapple go评论70阅读模式
英文:

LinkedList Add Two Numbers: LeetCode

问题

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int x = 0;
        int y = 0;
        int z = 1;

        while(l1 != null){
            x += z*l1.val;
            z*=10;
            l1 = l1.next;
        }
      
        z = 1;
        while(l2 != null){
            y += z*l2.val;
            z*=10;
            l2 = l2.next;
        }

        int sum = x + y;
        ListNode node = new ListNode(0);
        
        while(sum > 0){
            int digit = sum % 10;
            ListNode n = new ListNode(digit);
            while(node.next != null){
                node = node.next;
            }
            node.next = n;
            
            sum = sum / 10;
        }      
        return node;
    }
}
英文:

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.

The linked list seems to be overwriting the nodes as far as I can tell. I did find the answer to this problem on GeeksforGeeks but I wanted help with figuring out what is wrong with my code. I also know my code is not the best in regards to optimization but any halp is accepted. Thanks!

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
    class Solution {
        //ListNode head;
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int x = 0;
            int y = 0;
            int z = 1;
    
            while(l1 != null){
                x += z*l1.val;
                z*=10;
                l1 = l1.next;
            }
          
            z = 1;
            while(l2 != null){
                y += z*l2.val;
                z*=10;
                l2 = l2.next;
            }
    
            int sum = x + y;
            ListNode node = new ListNode(0);
            
            while(sum > 0){
                int digit = sum % 10;
                ListNode n = new ListNode(digit);
                while(node.next != null){
                    node = node.next;
                }
                node.next = n;
                
                sum = sum / 10;
    
                
            }      
            return node;
    
        }
    }

LeetCode Question

答案1

得分: 2

我喜欢你的解决方案但你的逻辑有点不完整

class Solution {
    //ListNode head;
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int x = 0;
        int y = 0;
        int z = 1;

        while(l1 != null){
            x += z*l1.val;
            z*=10;
            l1 = l1.next;
        }

        z = 1;
        while(l2 != null){
            y += z*l2.val;
            z*=10;
            l2 = l2.next;
        }

        int sum = x + y;
        if (sum == 0) {
            return new ListNode(0);
        }

        ListNode node = null, head = null;
        while(sum > 0){
            int digit = sum % 10;
            ListNode n = new ListNode(digit);
            if (node == null) {
                head = node = n;
            } else {
                node.next = n;
                node = node.next;
            }

            sum = sum / 10;
        }

        // 在 int sum = x + y; 后我只改变了一两处东西
        // 继续你的修改
        // ...
        
        return head;
    }
}
英文:

I like your solution but you have bit incomplete logic.

class Solution {
    //ListNode head;
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int x = 0;
        int y = 0;
        int z = 1;

        while(l1 != null){
            x += z*l1.val;
            z*=10;
            l1 = l1.next;
        }

        z = 1;
        while(l2 != null){
            y += z*l2.val;
            z*=10;
            l2 = l2.next;
        }

        int sum = x + y;
        if (sum == 0) {
            return new ListNode(0);
        }

        ListNode node = null, head = null;
        while(sum > 0){
            int digit = sum % 10;
            ListNode n = new ListNode(digit);
            if (node == null) {
                head = node = n;
            } else {
                node.next = n;
                node = node.next;
            }
            
            sum = sum / 10;
        }

        return head;
    }
}

I just changed one or two things after int sum = x + y;

答案2

得分: 0

以下是用 Kotlin 语言编写的解决方案。

class Solution {
    fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
        
        var head1 = l1
        var head2 = l2

        var result: ListNode? = null
        var temp: ListNode? = null

        var carry = 0

        while (head1 != null || head2 != null) {
            var sum = carry

            if (head1 != null) {
                sum += head1.`val`
                head1 = head1.next
            }

            if (head2 != null) {
                sum += head2.`val`
                head2 = head2.next
            }
             
            val node = ListNode(sum % 10)

            carry = sum / 10

            if (temp == null) {
                result = node
                temp = result
            } else {
                temp.next = node
                temp = temp.next
            }
        }

        if (carry > 0) {
            temp!!.next = ListNode(carry)
        }

        return result
    }
}
英文:

This is the solution in kotlin Language.

class Solution {
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
    
    var head1=l1
    var head2=l2

    var result:ListNode?=null
    var temp:ListNode?=null

    var carry=0

    while(head1 != null || head2 != null)
    {
        var sum=carry

       if(head1 != null)
       {

         sum +=  head1.`val`
         head1=head1.next

       }

       if(head2 != null)
       {
          sum +=head2.`val`
          head2= head2.next

       }
         
        val node= ListNode(sum%10)

        carry=sum/10

        if(temp== null)
        {

             result=node
             temp=result


        }

        else{ temp.next=node
        temp=temp.next}
       
    }

    if(carry>0){

        temp!!.next=ListNode(carry)
    }

return result
}
}

答案3

得分: 0

以下是您提供的Java代码的中文翻译:

/* 单链表的定义。
public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}*/

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int sum, carry;
        sum = l1.val + l2.val;
        carry = 0;
        if (sum >= 10){
            sum = sum - 10;
            carry = 1;
        }
        l1.val = sum;
        if(carry == 1){
            if(l1.next != null) {
                l1.next.val++;
            }
            else {
                l1.next = new ListNode(carry);
            } 
        }
        if(l1.next != null && l2.next!= null) addTwoNumbers(l1.next, l2.next);
        else if (l1.next!= null && l2.next == null) addTwoNumbers(l1.next, new ListNode(0));
        else if (l2.next != null && l1.next == null) {
            l1.next = new ListNode(0);
            addTwoNumbers(l1.next, l2.next);
        }
        return l1;
    }
}
英文:

The solution in Java from my leetcode submission. Beats 100% of other solutions in runtime, and beats 97.3% of solutions in memory. It uses recursion of course.

 /* Definition for singly-linked list.
 public class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }*/
 
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int sum, carry;
        sum = l1.val + l2.val;
        carry = 0;
        if (sum >= 10){
            sum = sum - 10;
            carry = 1;
        }
        l1.val = sum;
        if(carry == 1){
            if(l1.next != null) {
                l1.next.val++;
            }
            else {
                l1.next = new ListNode(carry);
            } 
        }
        if(l1.next != null && l2.next!= null) addTwoNumbers(l1.next, l2.next);
        else if (l1.next!= null && l2.next == null) addTwoNumbers(l1.next, new ListNode(0));
        else if (l2.next != null && l1.next == null) {
            l1.next = new ListNode(0);
            addTwoNumbers(l1.next, l2.next);
        }
        return l1;
    }
}

</details>



huangapple
  • 本文由 发表于 2020年9月22日 13:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64003658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定