From c6eb217d4613d869e147694d2652a07c2d10e4d6 Mon Sep 17 00:00:00 2001 From: liang-lh Date: Tue, 10 Feb 2026 04:06:23 +0800 Subject: [PATCH 1/4] Batch add 1 solutions: 1367.linked-list-in-... --- 1367.linked-list-in-binary-tree.java | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1367.linked-list-in-binary-tree.java diff --git a/1367.linked-list-in-binary-tree.java b/1367.linked-list-in-binary-tree.java new file mode 100644 index 00000000..0443527d --- /dev/null +++ b/1367.linked-list-in-binary-tree.java @@ -0,0 +1,44 @@ +# +# @lc app=leetcode id=1367 lang=java +# +# [1367] Linked List in Binary Tree +# +# @lc code=start +/** +* 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; } +* } +*/ +/** +* Definition for a binary tree node. +* public class TreeNode { +* int val; +* TreeNode left; +* TreeNode right; +* TreeNode() {} +* TreeNode(int val) { this.val = val; } +* TreeNode(int val, TreeNode left, TreeNode right) { +* this.val = val; +* this.left = left; +* this.right = right; +* } +* } +*/ +class Solution { + public boolean isSubPath(ListNode head, TreeNode root) { + if (root == null) return false; + return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right); + } + private boolean dfs(ListNode head, TreeNode node) { + if (head == null) return true; + if (node == null) return false; + if (node.val != head.val) return false; + return dfs(head.next, node.left) || dfs(head.next, node.right); + } +} +# @lc code=end \ No newline at end of file From 55c10810000caa9c0affcb2d61a3fb106fdc4b2e Mon Sep 17 00:00:00 2001 From: liang-lh Date: Tue, 10 Feb 2026 04:08:00 +0800 Subject: [PATCH 2/4] Batch add 1 solutions: 1206.design-skiplist... --- 1206.design-skiplist.java | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 1206.design-skiplist.java diff --git a/1206.design-skiplist.java b/1206.design-skiplist.java new file mode 100644 index 00000000..9ccafe24 --- /dev/null +++ b/1206.design-skiplist.java @@ -0,0 +1,98 @@ +# +# @lc app=leetcode id=1206 lang=java +# +# [1206] Design Skiplist +# +# @lc code=start +import java.util.Random; + +class Skiplist { + static final int MAX_LEVEL = 16; + static final double P = 0.5; + final Node head = new Node(-1, MAX_LEVEL); + int level = 1; + Random rand = new Random(); + + static class Node { + int val; + Node[] forward; + Node(int val, int level) { + this.val = val; + this.forward = new Node[level]; + } + } + + public Skiplist() {} + + private int randomLevel() { + int lvl = 1; + while (lvl < MAX_LEVEL && rand.nextDouble() < P) { + lvl++; + } + return lvl; + } + + public boolean search(int target) { + Node curr = head; + for (int i = level - 1; i >= 0; --i) { + while (curr.forward[i] != null && curr.forward[i].val < target) { + curr = curr.forward[i]; + } + } + curr = curr.forward[0]; + return curr != null && curr.val == target; + } + + public void add(int num) { + Node[] update = new Node[MAX_LEVEL]; + Node curr = head; + for (int i = level - 1; i >= 0; --i) { + while (curr.forward[i] != null && curr.forward[i].val < num) { + curr = curr.forward[i]; + } + update[i] = curr; + } + int lvl = randomLevel(); + if (lvl > level) { + for (int i = level; i < lvl; ++i) { + update[i] = head; + } + level = lvl; + } + Node newNode = new Node(num, lvl); + for (int i = 0; i < lvl; ++i) { + newNode.forward[i] = update[i].forward[i]; + update[i].forward[i] = newNode; + } + } + + public boolean erase(int num) { + Node[] update = new Node[MAX_LEVEL]; + Node curr = head; + for (int i = level - 1; i >= 0; --i) { + while (curr.forward[i] != null && curr.forward[i].val < num) { + curr = curr.forward[i]; + } + update[i] = curr; + } + curr = curr.forward[0]; + if (curr == null || curr.val != num) return false; + for (int i = 0; i < level; ++i) { + if (update[i].forward[i] != curr) break; + update[i].forward[i] = curr.forward[i]; + } + while (level > 1 && head.forward[level - 1] == null) { + level--; + } + return true; + } +} + +/** +* Your Skiplist object will be instantiated and called as such: +* Skiplist obj = new Skiplist(); +* boolean param_1 = obj.search(target); +* obj.add(num); +* boolean param_3 = obj.erase(num); +*/ +# @lc code=end \ No newline at end of file From 6434f7ba21dc7a452a8fa3411e536e5e0554d62a Mon Sep 17 00:00:00 2001 From: liang-lh Date: Tue, 10 Feb 2026 04:15:39 +0800 Subject: [PATCH 3/4] Batch add 1 solutions: 1367.linked-list-in-... --- 1367.linked-list-in-binary-tree.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/1367.linked-list-in-binary-tree.java b/1367.linked-list-in-binary-tree.java index 0443527d..58df8961 100644 --- a/1367.linked-list-in-binary-tree.java +++ b/1367.linked-list-in-binary-tree.java @@ -31,13 +31,15 @@ */ class Solution { public boolean isSubPath(ListNode head, TreeNode root) { + if (head == null) return true; if (root == null) return false; - return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right); + if (dfs(head, root)) return true; + return isSubPath(head, root.left) || isSubPath(head, root.right); } private boolean dfs(ListNode head, TreeNode node) { if (head == null) return true; if (node == null) return false; - if (node.val != head.val) return false; + if (head.val != node.val) return false; return dfs(head.next, node.left) || dfs(head.next, node.right); } } From e60445c52ca9fbb31abd385567626bf85f8d0ab2 Mon Sep 17 00:00:00 2001 From: liang-lh Date: Tue, 10 Feb 2026 04:23:56 +0800 Subject: [PATCH 4/4] Batch add 1 solutions: 1367.linked-list-in-... --- 1367.linked-list-in-binary-tree.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/1367.linked-list-in-binary-tree.java b/1367.linked-list-in-binary-tree.java index 58df8961..12cd9e1c 100644 --- a/1367.linked-list-in-binary-tree.java +++ b/1367.linked-list-in-binary-tree.java @@ -31,16 +31,15 @@ */ class Solution { public boolean isSubPath(ListNode head, TreeNode root) { - if (head == null) return true; if (root == null) return false; - if (dfs(head, root)) return true; + if (checkPath(head, root)) return true; return isSubPath(head, root.left) || isSubPath(head, root.right); } - private boolean dfs(ListNode head, TreeNode node) { + private boolean checkPath(ListNode head, TreeNode node) { if (head == null) return true; if (node == null) return false; if (head.val != node.val) return false; - return dfs(head.next, node.left) || dfs(head.next, node.right); + return checkPath(head.next, node.left) || checkPath(head.next, node.right); } } # @lc code=end \ No newline at end of file