File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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 doubleIt (ListNode head ) {
13
+ ListNode rev = reverse (head );
14
+ ListNode curr = rev ;
15
+ int carry = 0 ;
16
+ while (curr != null ) {
17
+ int val = curr .val * 2 + carry ;
18
+ carry = val / 10 ;
19
+ curr .val = val % 10 ;
20
+ if (curr .next == null && carry != 0 ) {
21
+ curr .next = new ListNode (carry );
22
+ break ;
23
+ }
24
+ curr = curr .next ;
25
+ }
26
+ return reverse (rev );
27
+ }
28
+
29
+ private ListNode reverse (ListNode curr ) {
30
+ ListNode next = null ;
31
+ ListNode prev = null ;
32
+ while (curr != null ) {
33
+ next = curr .next ;
34
+ curr .next = prev ;
35
+ prev = curr ;
36
+ curr = next ;
37
+ }
38
+ return prev ;
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments