-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd1toLL.java
More file actions
42 lines (35 loc) · 1.12 KB
/
add1toLL.java
File metadata and controls
42 lines (35 loc) · 1.12 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
class Solution {
public Node addOne(Node head) {
// Reverse the linked list
head = reverseList(head);
Node curr = head;
int carry = 1; // Start with the carry as 1 since we need to add 1
// Traverse the list and add the carry
while (curr != null) {
int sum = curr.data + carry;
curr.data = sum % 10;
carry = sum / 10;
// Move to the next node
if (curr.next == null && carry != 0) {
curr.next = new Node(carry); // Add a new node if carry is not zero and we're at the last node
break;
}
curr = curr.next;
}
// Reverse the list back to its original order
head = reverseList(head);
return head;
}
// Helper function to reverse the linked list
private Node reverseList(Node head) {
Node prev = null;
Node curr = head;
while (curr != null) {
Node nextNode = curr.next;
curr.next = prev;
prev = curr;
curr = nextNode;
}
return prev;
}
}