fork download
  1. /**
  2.  * Definition for singly-linked list.
  3.  * public class ListNode {
  4.  * int val;
  5.  * ListNode next;
  6.  * ListNode() {}
  7.  * ListNode(int val) { this.val = val; }
  8.  * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9.  * }
  10.  */
  11. class Solution {
  12. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  13.  
  14. int carry = 0;
  15.  
  16. ListNode t1 = l1;
  17. ListNode t2 = l2;
  18.  
  19. ListNode dummy = new ListNode(-1);
  20. ListNode mover = dummy;
  21.  
  22.  
  23. while (t1 != null || t2 != null || carry != 0) {
  24.  
  25. int val1 = 0;
  26.  
  27. if (t1 != null)
  28. val1 = t1.val;
  29.  
  30.  
  31. int val2 = 0;
  32.  
  33. if (t2 != null)
  34. val2 = t2.val;
  35.  
  36.  
  37. int sum = val1 + val2 + carry;
  38.  
  39. int data = sum % 10;
  40.  
  41. ListNode newNode = new ListNode(data);
  42.  
  43. mover.next = newNode;
  44. mover = mover.next;
  45.  
  46.  
  47. carry = sum / 10;
  48.  
  49.  
  50. if (t1 != null)
  51. t1 = t1.next;
  52.  
  53. if (t2 != null)
  54. t2 = t2.next;
  55. }
  56.  
  57.  
  58. return dummy.next;
  59. }
  60. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:12: error: cannot find symbol
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
                                  ^
  symbol:   class ListNode
  location: class Solution
Main.java:12: error: cannot find symbol
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
                                               ^
  symbol:   class ListNode
  location: class Solution
Main.java:12: error: cannot find symbol
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
           ^
  symbol:   class ListNode
  location: class Solution
Main.java:16: error: cannot find symbol
        ListNode t1 = l1;
        ^
  symbol:   class ListNode
  location: class Solution
Main.java:17: error: cannot find symbol
        ListNode t2 = l2;
        ^
  symbol:   class ListNode
  location: class Solution
Main.java:19: error: cannot find symbol
        ListNode dummy = new ListNode(-1);
        ^
  symbol:   class ListNode
  location: class Solution
Main.java:19: error: cannot find symbol
        ListNode dummy = new ListNode(-1);
                             ^
  symbol:   class ListNode
  location: class Solution
Main.java:20: error: cannot find symbol
        ListNode mover = dummy;
        ^
  symbol:   class ListNode
  location: class Solution
Main.java:41: error: cannot find symbol
            ListNode newNode = new ListNode(data);
            ^
  symbol:   class ListNode
  location: class Solution
Main.java:41: error: cannot find symbol
            ListNode newNode = new ListNode(data);
                                   ^
  symbol:   class ListNode
  location: class Solution
10 errors
stdout
Standard output is empty