Help me to find the key !


  • 0
    T

    I just want to know that why the solution I coding below doesn`t work, please.help me!

    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode ln = new ListNode(0);
           
            int temp;
            
            while(null != l1.next && null != l2.next){
                ln.val = ln.val + (l1.val + l2.val)%10;
                
                temp = (l1.val + l2.val)/10;
                
                ln.next = new ListNode(temp);
                
                ln = ln.next;
                
                l1 = l1.next;
                
                l2 = l2.next;
            }
            
            return ln;
        }
    }
    

  • 0
    S

    In my opinion,firstly,your return sentence is wrong.Because your ln pointer has pointed the tail of the list.So you should state another pointer.For example:below your third line to add ListNode test = ln;finally,return test;then,you should modify your while()


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.