Skip to content

Commit

Permalink
fix DataStructure/ListNode.java, relative code, add 369 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 22, 2024
1 parent c8a755e commit 71bcc91
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 25 deletions.
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-12-22
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-12-21
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20241222: 369
20241221: 370
20241220: 815,871,593,1109
20241214: 560,523
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@

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

public class ListNode{

// attr
// V1
//public class ListNode{
//
// // attr
// public int val;
// public ListNode next;
//
// // constructor
// public ListNode(){
//
// }
//
// public ListNode(int val){
// this.val = val;
// }
//
// ListNode(int val, ListNode next){
// this.val = val;
// this.next = next;
// }
//
//}

// V2
public class ListNode {
public int val;
public ListNode next;

// constructor
public ListNode(){

}

public ListNode(int val){
this.val = val;
}

ListNode(int val, ListNode next){
this.val = val;
this.next = next;
}

public ListNode() {}
public ListNode(int val) { this.val = val; }
public ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

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

import LeetCodeJava.DataStructure.ListNode;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
//public class ListNode {
// int val;
// ListNode next;
// ListNode() {}
// ListNode(int val) { this.val = val; }
// ListNode(int val, ListNode next) { this.val = val; this.next = next; }
// }

public class MergeKSortedLists {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package LeetCodeJava.LinkedList;

// https://leetcode.com/problems/plus-one-linked-list/description/
// https://leetcode.ca/all/369.html

import LeetCodeJava.DataStructure.ListNode;

/**
* 369. Plus One Linked List Given a non-negative integer represented as non-empty a singly linked
* list of digits, plus one to the integer.
*
* <p>You may assume the integer do not contain any leading zero, except the number 0 itself.
*
* <p>The digits are stored such that the most significant digit is at the head of the list.
*
* <p>Example :
*
* <p>Input: [1,2,3] Output: [1,2,4] Difficulty: Medium Lock: Prime Company: Amazon Apple Google
*/
public class PlusOneLinkedList {

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/

// V0
// public ListNode plusOne(ListNode head) {
// }

// V1
// https://leetcode.ca/2016-12-03-369-Plus-One-Linked-List/
public ListNode plusOne_1(ListNode head) {
ListNode dummy = new ListNode(0, head);
ListNode target = dummy;
while (head != null) {
if (head.val != 9) {
target = head;
}
head = head.next;
}
++target.val;
target = target.next;
while (target != null) {
target.val = 0;
target = target.next;
}
return dummy.val == 1 ? dummy : dummy.next;
}

// V2
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// https://leetcode.com/problems/reverse-nodes-in-k-group/description/
// https://neetcode.io/problems/reverse-nodes-in-k-group

import LeetCodeJava.DataStructure.ListNode;

public class ReverseNodesInKGroup {

// V0
Expand Down
62 changes: 62 additions & 0 deletions leetcode_java/src/main/java/dev/workspace6.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev;


import LeetCodeJava.DataStructure.ListNode;

import javax.print.DocFlavor;
import java.util.*;

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

// LC 369
// https://leetcode.ca/all/369.html
// 3.42 PM - 4.00 pm
/**
* IDEA 1 : linked list -> int, +1, transform back to linked list
*
* Idea 2: do in linked list directly
* -> reverse
* -> add 1, if > 9, move to next digit
* -> reverse again
*
*/
// v2
public ListNode plusOne(ListNode head) {
if (head == null){
return new ListNode(1); // ??
}
ListNode tmp = new ListNode();
// // reverse
// while(head != null){
// ListNode _next = head.next;
// ListNode _prev = tmp; // ??
// head.next = _prev;
// head = _next;
// }
// tmp.val += 1; // ??


return null;
}

// v1
// public ListNode plusOne_1(ListNode head) {
// if (head == null){
// return new ListNode(1); // ??
// }
// List<Integer> list = new ArrayList<>();
// while(head != null){
// list.add(head.val);
// head = head.next;
// }
// // ???
// int cur = 0;
// for (int j = list.size(); j >= 0; j--){
// cur += (10 ^ j) * list.get(j);
// }
// cur += 1;
// ListNode res = new ListNode();
// String string = String.valueOf(cur);
// for (String x : string.split("")){
// ListNode tmp = new ListNode();
// tmp.val = Integer.parseInt(x);
// res.next = tmp;
// res = res.next;
// }
//
// return res;
// }

}

0 comments on commit 71bcc91

Please sign in to comment.