Skip to content

Commit 71bcc91

Browse files
committed
fix DataStructure/ListNode.java, relative code, add 369 java
1 parent c8a755e commit 71bcc91

File tree

7 files changed

+163
-25
lines changed

7 files changed

+163
-25
lines changed

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-12-22
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-12-21
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241222: 369
12
20241221: 370
23
20241220: 815,871,593,1109
34
20241214: 560,523

leetcode_java/src/main/java/LeetCodeJava/DataStructure/ListNode.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,34 @@
22

33
// https://leetcode.com/problems/merge-two-sorted-lists/
44

5-
public class ListNode{
6-
7-
// attr
5+
// V1
6+
//public class ListNode{
7+
//
8+
// // attr
9+
// public int val;
10+
// public ListNode next;
11+
//
12+
// // constructor
13+
// public ListNode(){
14+
//
15+
// }
16+
//
17+
// public ListNode(int val){
18+
// this.val = val;
19+
// }
20+
//
21+
// ListNode(int val, ListNode next){
22+
// this.val = val;
23+
// this.next = next;
24+
// }
25+
//
26+
//}
27+
28+
// V2
29+
public class ListNode {
830
public int val;
931
public ListNode next;
10-
11-
// constructor
12-
public ListNode(){
13-
14-
}
15-
16-
public ListNode(int val){
17-
this.val = val;
18-
}
19-
20-
ListNode(int val, ListNode next){
21-
this.val = val;
22-
this.next = next;
23-
}
24-
32+
public ListNode() {}
33+
public ListNode(int val) { this.val = val; }
34+
public ListNode(int val, ListNode next) { this.val = val; this.next = next; }
2535
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
// https://leetcode.com/problems/merge-k-sorted-lists/
44

5+
import LeetCodeJava.DataStructure.ListNode;
6+
57
import java.util.ArrayList;
68
import java.util.Comparator;
79
import java.util.List;
810

911

10-
class ListNode {
11-
int val;
12-
ListNode next;
13-
ListNode() {}
14-
ListNode(int val) { this.val = val; }
15-
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16-
}
12+
//public class ListNode {
13+
// int val;
14+
// ListNode next;
15+
// ListNode() {}
16+
// ListNode(int val) { this.val = val; }
17+
// ListNode(int val, ListNode next) { this.val = val; this.next = next; }
18+
// }
1719

1820
public class MergeKSortedLists {
1921

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package LeetCodeJava.LinkedList;
2+
3+
// https://leetcode.com/problems/plus-one-linked-list/description/
4+
// https://leetcode.ca/all/369.html
5+
6+
import LeetCodeJava.DataStructure.ListNode;
7+
8+
/**
9+
* 369. Plus One Linked List Given a non-negative integer represented as non-empty a singly linked
10+
* list of digits, plus one to the integer.
11+
*
12+
* <p>You may assume the integer do not contain any leading zero, except the number 0 itself.
13+
*
14+
* <p>The digits are stored such that the most significant digit is at the head of the list.
15+
*
16+
* <p>Example :
17+
*
18+
* <p>Input: [1,2,3] Output: [1,2,4] Difficulty: Medium Lock: Prime Company: Amazon Apple Google
19+
*/
20+
public class PlusOneLinkedList {
21+
22+
/**
23+
* Definition for singly-linked list.
24+
* public class ListNode {
25+
* int val;
26+
* ListNode next;
27+
* ListNode() {}
28+
* ListNode(int val) { this.val = val; }
29+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
30+
* }
31+
*/
32+
33+
// V0
34+
// public ListNode plusOne(ListNode head) {
35+
// }
36+
37+
// V1
38+
// https://leetcode.ca/2016-12-03-369-Plus-One-Linked-List/
39+
public ListNode plusOne_1(ListNode head) {
40+
ListNode dummy = new ListNode(0, head);
41+
ListNode target = dummy;
42+
while (head != null) {
43+
if (head.val != 9) {
44+
target = head;
45+
}
46+
head = head.next;
47+
}
48+
++target.val;
49+
target = target.next;
50+
while (target != null) {
51+
target.val = 0;
52+
target = target.next;
53+
}
54+
return dummy.val == 1 ? dummy : dummy.next;
55+
}
56+
57+
// V2
58+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// https://leetcode.com/problems/reverse-nodes-in-k-group/description/
44
// https://neetcode.io/problems/reverse-nodes-in-k-group
55

6+
import LeetCodeJava.DataStructure.ListNode;
7+
68
public class ReverseNodesInKGroup {
79

810
// V0

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package dev;
22

3+
4+
import LeetCodeJava.DataStructure.ListNode;
5+
36
import javax.print.DocFlavor;
47
import java.util.*;
58

@@ -481,4 +484,63 @@ public int[] getModifiedArray(int length, int[][] updates) {
481484
return Arrays.copyOfRange(tmp, 0, length); // ????
482485
}
483486

487+
// LC 369
488+
// https://leetcode.ca/all/369.html
489+
// 3.42 PM - 4.00 pm
490+
/**
491+
* IDEA 1 : linked list -> int, +1, transform back to linked list
492+
*
493+
* Idea 2: do in linked list directly
494+
* -> reverse
495+
* -> add 1, if > 9, move to next digit
496+
* -> reverse again
497+
*
498+
*/
499+
// v2
500+
public ListNode plusOne(ListNode head) {
501+
if (head == null){
502+
return new ListNode(1); // ??
503+
}
504+
ListNode tmp = new ListNode();
505+
// // reverse
506+
// while(head != null){
507+
// ListNode _next = head.next;
508+
// ListNode _prev = tmp; // ??
509+
// head.next = _prev;
510+
// head = _next;
511+
// }
512+
// tmp.val += 1; // ??
513+
514+
515+
return null;
516+
}
517+
518+
// v1
519+
// public ListNode plusOne_1(ListNode head) {
520+
// if (head == null){
521+
// return new ListNode(1); // ??
522+
// }
523+
// List<Integer> list = new ArrayList<>();
524+
// while(head != null){
525+
// list.add(head.val);
526+
// head = head.next;
527+
// }
528+
// // ???
529+
// int cur = 0;
530+
// for (int j = list.size(); j >= 0; j--){
531+
// cur += (10 ^ j) * list.get(j);
532+
// }
533+
// cur += 1;
534+
// ListNode res = new ListNode();
535+
// String string = String.valueOf(cur);
536+
// for (String x : string.split("")){
537+
// ListNode tmp = new ListNode();
538+
// tmp.val = Integer.parseInt(x);
539+
// res.next = tmp;
540+
// res = res.next;
541+
// }
542+
//
543+
// return res;
544+
// }
545+
484546
}

0 commit comments

Comments
 (0)