-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-1-to-a-number-represented-as-linked-list.java
48 lines (40 loc) · 1.36 KB
/
add-1-to-a-number-represented-as-linked-list.java
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
class Solution {
// Function to add 1 to a number represented as a linked list
public Node addOne(Node head) {
// Step 1: Reverse the linked list
head = reverseList(head);
// Step 2: Add 1 to the reversed list
Node current = head;
int carry = 1;
while (current != null) {
int sum = current.data + carry;
current.data = sum % 10;
carry = sum / 10;
// If no carry left, break out of the loop
if (carry == 0) break;
// Move to the next node
if (current.next == null && carry > 0) {
current.next = new Node(carry);
carry = 0;
}
current = current.next;
}
// Step 3: Reverse the list back to original order
head = reverseList(head);
return head;
}
// Helper function to reverse the linked list
private Node reverseList(Node head) {
Node prev = null;
Node current = head;
Node next = null;
while (current != null) {
next = current.next; // Store next
current.next = prev; // Reverse current node's pointer
prev = current; // Move pointers one position ahead
current = next;
}
head = prev;
return head;
}
}