-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddTwoNumbers.java
More file actions
72 lines (68 loc) · 2.18 KB
/
Copy pathaddTwoNumbers.java
File metadata and controls
72 lines (68 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//Init conditions
if(l1==null && l2==null){return null;}
// reverse the two lists
ListNode reversedList1 = reverseList(l1);
ListNode reversedList2 = reverseList(l2);
//parse the two lists
String parseL1 = parse(reversedList1);
String parseL2 = parse(reversedList2);
//add the two list
long intResult = Long.parseLong(parseL1) + Long.parseLong(parseL2);
//build the result list
return reverseList(listOf(String.valueOf(intResult)));
}
public ListNode listOf(String str){
ListNode result = new ListNode(0);
ListNode iterator = result;
String[] substrings = str.split("");
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
iterator.val = Character.getNumericValue(ch[i]);
if(i!=ch.length-1){
iterator.next = new ListNode(0);
iterator = iterator.next;
}
}
return result;
}
public String parse(ListNode list){
ListNode iterator = list;
String result ="";
if(iterator==null){return null;}
while(iterator!=null){
result = result + String.valueOf(iterator.val);
iterator = iterator.next;
}
return result;
}
public ListNode reverseList(ListNode head) {
if(head == null){return null;}
ListNode prev = null;
ListNode cur = head;
head = head.next;
return recursiveReverseList(prev,cur,head);
}
public ListNode recursiveReverseList(ListNode prev, ListNode cur, ListNode head)
{
if(head == null)
{
cur.next =prev;
return cur;
}
head = cur.next;
cur.next = prev;
return recursiveReverseList(cur, head, head.next);
}
}