Skip to content

Commit 0d1b3d8

Browse files
committed
add 369 java
1 parent 71bcc91 commit 0d1b3d8

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@
417417
234| [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)|[Python](./leetcode_python/Linked_list/palindrome-linked-list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/PalindromeLinkedList.java)| _O(n)_ | _O(1)_| Easy|linked list,`amazon`,`fb`| OK (4)
418418
237| [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/)| [Python](./leetcode_python/Linked_list/delete-node-in-a-linked-list.py)| _O(1)_| _O(1)_| Easy| LintCode, apple | OK * (1) (but again)
419419
328| [Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/)|[Python](./leetcode_python/Linked_list/odd-even-linked-list.py) | _O(n)_ | _O(1)_ | Medium |`basic` | OK** (2)
420-
369| [Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/)| [Python](./leetcode_python/Linked_list/plus-one-linked-list.py) | _O(n)_ | _O(1)_ | Medium | 🔒, `basic`, `trick` | AGAIN* (2)
420+
369| [Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/)| [Python](./leetcode_python/Linked_list/plus-one-linked-list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/PlusOneLinkedList.java) | _O(n)_ | _O(1)_ | Medium | 🔒, linkedlist, `basic`, google | AGAIN****** (3)
421421
445| [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/)| [Python](./leetcode_python/Linked_list/add-two-numbers-ii.py) | _O(m + n)_ | _O(m + n)_| Medium |`trick`, linked list, string,`good basic`, `amazon`| AGAIN*** (3)
422422
725 | [Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/) | [Python](./leetcode_python/Linked_list/split-linked-list-in-parts.py) | _O(n + k)_ | _O(1)_ | Medium |mod, split linked list, linked list, good trick,`amazon`| AGAIN************ (6) (again)
423423
817 | [Linked List Components](https://leetcode.com/problems/linked-list-components/) | [Python](./leetcode_python/Linked_list/linked-list-components.py) | _O(m + n)_ | _O(m)_ | Medium || OK*

data/to_review.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
2025-02-15 -> ['369']
12
2025-02-14 -> ['370']
23
2025-02-13 -> ['815,871,593,1109']
34
2025-02-07 -> ['560,523']
45
2025-02-01 -> ['304,853,325']
56
2025-01-26 -> ['370(todo)']
7+
2025-01-25 -> ['369']
68
2025-01-24 -> ['370', '34,767']
79
2025-01-23 -> ['815,871,593,1109']
810
2025-01-20 -> ['722,380']
@@ -11,20 +13,21 @@
1113
2025-01-16 -> ['776,31']
1214
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
1315
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
16+
2025-01-12 -> ['369']
1417
2025-01-11 -> ['370', '304,853,325', '394']
1518
2025-01-10 -> ['815,871,593,1109', '833,950']
1619
2025-01-05 -> ['370(todo)']
17-
2025-01-04 -> ['560,523', '53,210,207']
20+
2025-01-04 -> ['369', '560,523', '53,210,207']
1821
2025-01-03 -> ['370', '34,767', '444']
1922
2025-01-02 -> ['815,871,593,1109', '1188,130,855(again)']
20-
2024-12-30 -> ['722,380']
23+
2024-12-30 -> ['369', '722,380']
2124
2024-12-29 -> ['370', '304,853,325', '33,81']
2225
2024-12-28 -> ['815,871,593,1109', '900']
23-
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
26+
2024-12-27 -> ['369', '560,523', '253', '26,27', '802,1197,26']
2427
2024-12-26 -> ['370', '776,31']
25-
2024-12-25 -> ['815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
26-
2024-12-24 -> ['370', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
27-
2024-12-23 -> ['370', '815,871,593,1109', '370(todo)']
28+
2024-12-25 -> ['369', '815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
29+
2024-12-24 -> ['369', '370', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
30+
2024-12-23 -> ['369', '370', '815,871,593,1109', '370(todo)']
2831
2024-12-22 -> ['370', '815,871,593,1109', '560,523']
2932
2024-12-21 -> ['815,871,593,1109', '304,853,325', '34,767', '394', '855,846']
3033
2024-12-20 -> ['833,950', '932']

leetcode_java/src/main/java/LeetCodeJava/LinkedList/PlusOneLinkedList.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,61 @@ public class PlusOneLinkedList {
3131
*/
3232

3333
// V0
34+
// TODO : implement
3435
// public ListNode plusOne(ListNode head) {
3536
// }
3637

38+
3739
// V1
38-
// https://leetcode.ca/2016-12-03-369-Plus-One-Linked-List/
40+
// IDEA : LINKED LIST OP (gpt)
41+
/**
42+
* Step 1) reverse linked list
43+
* Step 2) plus 1, bring `carry` to next digit if curSum > 9, ... repeat for all nodes
44+
* Step 3) reverse linked list again
45+
*/
3946
public ListNode plusOne_1(ListNode head) {
47+
if (head == null) return new ListNode(1); // Handle edge case
48+
49+
// Reverse the linked list
50+
head = reverseList(head);
51+
52+
// Add one to the reversed list
53+
ListNode current = head;
54+
int carry = 1; // Start with adding one
55+
56+
while (current != null && carry > 0) {
57+
int sum = current.val + carry;
58+
current.val = sum % 10; // Update the current node value
59+
carry = sum / 10; // Calculate carry for the next node
60+
if (current.next == null && carry > 0) {
61+
current.next = new ListNode(carry); // Add a new node for carry
62+
carry = 0; // No more carry after this
63+
}
64+
current = current.next;
65+
}
66+
67+
// Reverse the list back to original order
68+
return reverseList(head);
69+
}
70+
71+
// Utility to reverse a linked list
72+
private ListNode reverseList(ListNode head) {
73+
ListNode prev = null;
74+
ListNode current = head;
75+
76+
while (current != null) {
77+
ListNode next = current.next; // Save the next node
78+
current.next = prev; // Reverse the link
79+
prev = current; // Move prev forward
80+
current = next; // Move current forward
81+
}
82+
83+
return prev;
84+
}
85+
86+
// V2
87+
// https://leetcode.ca/2016-12-03-369-Plus-One-Linked-List/
88+
public ListNode plusOne_2(ListNode head) {
4089
ListNode dummy = new ListNode(0, head);
4190
ListNode target = dummy;
4291
while (head != null) {
@@ -54,5 +103,5 @@ public ListNode plusOne_1(ListNode head) {
54103
return dummy.val == 1 ? dummy : dummy.next;
55104
}
56105

57-
// V2
106+
// V3
58107
}

0 commit comments

Comments
 (0)