Skip to content

Instantly share code, notes, and snippets.

@darthcoder
Created June 9, 2018 22:35
Show Gist options
  • Select an option

  • Save darthcoder/9f4bed46e6e63967afea0d51a72d383d to your computer and use it in GitHub Desktop.

Select an option

Save darthcoder/9f4bed46e6e63967afea0d51a72d383d to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
// public class ListNode {
// int val;
// ListNode next;
// public ListNode(int x) { val = x; }
// }
// input is a ListNode with digits as elements
public int pop(ListNode l) {
int i = 1;
int sum = 0;
while(l.next != null) {
sum += i*l.val;
i *= 10;
}
return sum;
}
public ListNode num2list(int number) {
ListNode l = null;
while (number > 0) {
l.val = number % 10;
l = l.next;
number = number / 10;
}
return l;
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int n1 = pop(l1);
int n2 = pop(l2);
int n = n1 + n2;
return num2list(n);
}
//public static void main (String[] args) {
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment