works on eclipse but not in online editor


  • 0
    G

    class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
    }

    class Solution {

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode res=null;
    ListNode pres=null;
    ListNode head=null;
    int c=0;
    while(l1!=null && l2!=null){
    int s= l1.val + l2.val+c;
    if (s>=10){
    s=s-10;
    c=1;
    }
    else c=0;
    ListNode temp= new ListNode(s);
    l1=l1.next;
    l2=l2.next;
    if (res==null){
    res=temp;
    head=res;
    }
    else {pres.next=temp;}
    pres=temp;

    }
    return head;
    }
    public static void main(String[] args) {
    ListNode result=null;
    ListNode l1 = new ListNode(2);
    l1.next = new ListNode(4);
    l1.next.next = new ListNode(3);

       ListNode l2 = new ListNode(5);
       l2.next = new ListNode(6);
       l2.next.next = new ListNode(4);
      
      result=addTwoNumbers(l1,l2);
      
      ListNode curr=result;
      while(curr!=null){
    	  System.out.println(curr.val);
    	  curr=curr.next;
      }
    

    }

    }


Log in to reply
 

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