-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleANumberRepresentedAsALinkedList2816.java
More file actions
54 lines (49 loc) · 1.45 KB
/
Copy pathDoubleANumberRepresentedAsALinkedList2816.java
File metadata and controls
54 lines (49 loc) · 1.45 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
/*
You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes.
Return the head of the linked list after doubling it.
*/
public class DoubleANumberRepresentedAsALinkedList2816 {
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode node1 = new ListNode(2);
head.next = node1;
ListNode node2 = new ListNode(3);
node1.next = node2;
node2.next = new ListNode(9);
ListNode node = head;
while (node != null) {
System.out.print(node.val + " ");
node = node.next;
}
System.out.println();
head = doubleIt(head);
node = head;
while (node != null) {
System.out.print(node.val + " ");
node = node.next;
}
}
public static ListNode doubleIt(ListNode head) {
if (head.val > 4) head = new ListNode(0, head);
ListNode node = head;
while (node != null) {
node.val = (node.val * 2) % 10;
if (node.next != null && node.next.val > 4) node.val++;
node = node.next;
}
return head;
}
public static class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
}