Accepted Java solution


  • 1
    M
     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    	    	  
    	    	
    	    	  String s=new String();
    	    	  String s1 = new String();
    	    	  s1="";
        		  s="";
    	    	  while(l1 != null){
    	    		  s=s.concat(s.valueOf(l1.val));
    	    		  l1=l1.next;
    	    	  }		  
    	    	  StringBuilder sb = new StringBuilder(s);
    	    	    s = sb.reverse().toString();
    	    	    	    
    	    	  while(l2!=null){
    	    		  s1=s1.concat(s.valueOf(l2.val));
    	    		   l2=l2.next;
    	    	  }
    	    	  sb= new StringBuilder(s1);
    	    	  s1=sb.reverse().toString();
    	    	   long a=Long.parseLong(s);
    	    	  long b= Long.parseLong(s1);
    	    	  long c= a+b;
    	    	
    	    	  ListNode output = new ListNode(0);
    	    	  ListNode finalOutput = new ListNode(0);
    	    	  finalOutput = output;
    	    	  while(c/10 != 0){	    		  
    	    	  output.val=(int)(c%10);
    	    	   	  c=c/10;
    	    	  
    	    	  output.next=new ListNode(0);
    	    	  output=output.next;
    	    	  }
    	    	 
    	    	  output.val=(int)c;
    	    	    	  
    			return finalOutput;
    	          
    	      }

Log in to reply
 

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