Skip to content

Commit

Permalink
add 369 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 22, 2024
1 parent 71bcc91 commit 0d1b3d8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@
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)
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)
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)
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)
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)
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)
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)
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*
Expand Down
15 changes: 9 additions & 6 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
2025-02-15 -> ['369']
2025-02-14 -> ['370']
2025-02-13 -> ['815,871,593,1109']
2025-02-07 -> ['560,523']
2025-02-01 -> ['304,853,325']
2025-01-26 -> ['370(todo)']
2025-01-25 -> ['369']
2025-01-24 -> ['370', '34,767']
2025-01-23 -> ['815,871,593,1109']
2025-01-20 -> ['722,380']
Expand All @@ -11,20 +13,21 @@
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-12 -> ['369']
2025-01-11 -> ['370', '304,853,325', '394']
2025-01-10 -> ['815,871,593,1109', '833,950']
2025-01-05 -> ['370(todo)']
2025-01-04 -> ['560,523', '53,210,207']
2025-01-04 -> ['369', '560,523', '53,210,207']
2025-01-03 -> ['370', '34,767', '444']
2025-01-02 -> ['815,871,593,1109', '1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-30 -> ['369', '722,380']
2024-12-29 -> ['370', '304,853,325', '33,81']
2024-12-28 -> ['815,871,593,1109', '900']
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2024-12-27 -> ['369', '560,523', '253', '26,27', '802,1197,26']
2024-12-26 -> ['370', '776,31']
2024-12-25 -> ['815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['370', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-23 -> ['370', '815,871,593,1109', '370(todo)']
2024-12-25 -> ['369', '815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
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)']
2024-12-23 -> ['369', '370', '815,871,593,1109', '370(todo)']
2024-12-22 -> ['370', '815,871,593,1109', '560,523']
2024-12-21 -> ['815,871,593,1109', '304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['833,950', '932']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,61 @@ public class PlusOneLinkedList {
*/

// V0
// TODO : implement
// public ListNode plusOne(ListNode head) {
// }


// V1
// https://leetcode.ca/2016-12-03-369-Plus-One-Linked-List/
// IDEA : LINKED LIST OP (gpt)
/**
* Step 1) reverse linked list
* Step 2) plus 1, bring `carry` to next digit if curSum > 9, ... repeat for all nodes
* Step 3) reverse linked list again
*/
public ListNode plusOne_1(ListNode head) {
if (head == null) return new ListNode(1); // Handle edge case

// Reverse the linked list
head = reverseList(head);

// Add one to the reversed list
ListNode current = head;
int carry = 1; // Start with adding one

while (current != null && carry > 0) {
int sum = current.val + carry;
current.val = sum % 10; // Update the current node value
carry = sum / 10; // Calculate carry for the next node
if (current.next == null && carry > 0) {
current.next = new ListNode(carry); // Add a new node for carry
carry = 0; // No more carry after this
}
current = current.next;
}

// Reverse the list back to original order
return reverseList(head);
}

// Utility to reverse a linked list
private ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode current = head;

while (current != null) {
ListNode next = current.next; // Save the next node
current.next = prev; // Reverse the link
prev = current; // Move prev forward
current = next; // Move current forward
}

return prev;
}

// V2
// https://leetcode.ca/2016-12-03-369-Plus-One-Linked-List/
public ListNode plusOne_2(ListNode head) {
ListNode dummy = new ListNode(0, head);
ListNode target = dummy;
while (head != null) {
Expand All @@ -54,5 +103,5 @@ public ListNode plusOne_1(ListNode head) {
return dummy.val == 1 ? dummy : dummy.next;
}

// V2
// V3
}

0 comments on commit 0d1b3d8

Please sign in to comment.