4ms 11lines java solution


  • 26
    S
    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode ln1 = l1, ln2 = l2, head = null, node = null;
            int carry = 0, remainder = 0, sum = 0;
            head = node = new ListNode(0);
            
            while(ln1 != null || ln2 != null || carry != 0) {
                sum = (ln1 != null ? ln1.val : 0) + (ln2 != null ? ln2.val : 0) + carry;
                carry = sum / 10;
                remainder = sum % 10;
                node = node.next = new ListNode(remainder);
                ln1 = (ln1 != null ? ln1.next : null);
                ln2 = (ln2 != null ? ln2.next : null);
            }
            return head.next;
        }
    }

  • -4
    L
    This post is deleted!

  • 0

    if won't be like you said!cause he just add every digit of two list node one by one,so in a loop ,sum would between 0 and 18


  • 0
    L

    between 0 and 19


  • 7
    L

    @swgrhck I have ran your code for several times, all the runtime are around 50ms.


  • 4
    Y

    the runtime is around 50ms not like what you said "4ms"


  • 2

    @swgrhck
    I guess when u post this they haven't add so many test cases,
    it doesn't pass with this one

    [9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]
    [1]
    

    here is my solution

        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode dum = new ListNode(-1), curt = null;
            int sum = 0, carry = 0;
            curt = dum;
            while (l1 != null || l2 != null || carry != 0) {
                sum = 0;
                if (l1 != null) {
                    sum += l1.val;
                    l1 = l1.next;
                }
                if (l2 != null) {
                    sum += l2.val;
                    l2 = l2.next;
                }
                sum += carry;
                carry = sum / 10;
                ListNode temp = new ListNode(sum % 10);
                curt.next = temp;
                curt = curt.next;
            }
            return dum.next;
        }

  • 0
    J

    please explain here what happens

    case 1= 2 + 5 = 7
    sum += carry; = 7
    carry = sum / 10; = 0
    7 % 10 = 7

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


  • 0
    X

    @Lara I do not see much difference with the best solution. Also, I test by the following code. The cost time is similar.

    public class AddTwoNumber {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    	ListNode dum = new ListNode(-1), curt = null;
    	int sum = 0, carry = 0;
    	curt = dum;
    	while (l1 != null || l2 != null || carry != 0) {
    		sum = 0;
    		if (l1 != null) {
    			sum += l1.val;
    			l1 = l1.next;
    		}
    		if (l2 != null) {
    			sum += l2.val;
    			l2 = l2.next;
    		}
    		sum += carry;
    		carry = sum / 10;
    		ListNode temp = new ListNode(sum % 10);
    		curt.next = temp;
    		curt = curt.next;
    	}
    	return dum.next;
    }
    
    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
    	ListNode ln1 = l1, ln2 = l2, head = null, node = null;
    	int carry = 0, remainder = 0, sum = 0;
    	head = node = new ListNode(0);
    
    	while (ln1 != null || ln2 != null || carry != 0) {
    		sum = (ln1 != null ? ln1.val : 0) + (ln2 != null ? ln2.val : 0) + carry;
    		carry = sum / 10;
    		remainder = sum % 10;
    		node = node.next = new ListNode(remainder);
    		ln1 = (ln1 != null ? ln1.next : null);
    		ln2 = (ln2 != null ? ln2.next : null);
    	}
    	return head.next;
    }
    
    public static ListNode createNode(String sentence) {
    	ListNode head = new ListNode(0);
    	for (int i = 0; i < sentence.length(); i++) {
    		char c = sentence.charAt(i);
    		if (c >= '0' && c <= '9') {
    			ListNode newNode = new ListNode(c - '0');
    			newNode.next = head.next;
    			head.next = newNode;
    		}
    	}
    	return head.next;
    }
    
    /**
     * [9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]
     * [1]
     * 
     * @param args
     */
    public static void main(String[] args) {
    	String s1 = "[9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]";
    	String s2 ="[1]";
    	ListNode l1 = createNode(s1);
    	ListNode l2 = createNode(s2);
    	
    	long startTime=System.nanoTime();
    	ListNode mergeL3 = new AddTwoNumber().addTwoNumbers(l1, l2);
    	long endTime=System.nanoTime();
    	System.out.println("程序运行时间: "+(endTime-startTime)+"ns");
    	
    	startTime=System.nanoTime();
    	ListNode mergeL4 = new AddTwoNumber().addTwoNumbers2(l1, l2);
    	endTime=System.nanoTime();
    	System.out.println("程序运行时间: "+(endTime-startTime)+"ns");
    	
    }
    

    }


  • 1
    N

    @swgrhck Good solution. Could be simplified somewhat by making the first line in the method
    ListNode ln1 = l1, ln2 = l2, head = new ListNode(0), node = head; and then removing the third line


  • 0
    X

    @swgrhck Nice solution.


  • 0
    R
    This post is deleted!

Log in to reply
 

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