feat: 2025-10-29打卡
This commit is contained in:
66
23.merge-k-sorted-lists.java
Normal file
66
23.merge-k-sorted-lists.java
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=23 lang=java
|
||||||
|
*
|
||||||
|
* [23] Merge k Sorted Lists
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @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; }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
public ListNode mergeKLists(ListNode[] lists) {
|
||||||
|
if(lists == null || lists.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return mergeRecursively(lists, 0, lists.length-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ListNode mergeRecursively(ListNode[] lists, int i, int j) {
|
||||||
|
if (i > j) {
|
||||||
|
return null;
|
||||||
|
}else if(i == j) {
|
||||||
|
return lists[i];
|
||||||
|
} else if (j - i == 1) {
|
||||||
|
return merge(lists[i], lists[j]);
|
||||||
|
}
|
||||||
|
int m = (i+j)/2;
|
||||||
|
return merge(mergeRecursively(lists, i, m), mergeRecursively(lists, m+1, j));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ListNode merge(ListNode l1, ListNode l2) {
|
||||||
|
ListNode dummy = new ListNode(0), tail = dummy;
|
||||||
|
dummy.next = null;
|
||||||
|
while(l1 != null || l2 != null) {
|
||||||
|
if(l1 == null || l2 == null) {
|
||||||
|
if(l1 != null) {
|
||||||
|
tail.next = l1;
|
||||||
|
} else if(l2 != null) {
|
||||||
|
tail.next = l2;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(l1.val <= l2.val) {
|
||||||
|
tail.next = l1;
|
||||||
|
l1 = l1.next;
|
||||||
|
tail = tail.next;
|
||||||
|
tail.next = null;
|
||||||
|
} else {
|
||||||
|
tail.next = l2;
|
||||||
|
l2 = l2.next;
|
||||||
|
tail = tail.next;
|
||||||
|
tail.next = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dummy.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
48
25.reverse-nodes-in-k-group.java
Normal file
48
25.reverse-nodes-in-k-group.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=25 lang=java
|
||||||
|
*
|
||||||
|
* [25] Reverse Nodes in k-Group
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @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; }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
public ListNode reverseKGroup(ListNode head, int k) {
|
||||||
|
ListNode dummy = new ListNode(0), prev, frm, to;
|
||||||
|
dummy.next = head;
|
||||||
|
prev = dummy;
|
||||||
|
while(prev.next != null) {
|
||||||
|
frm = to = prev.next;
|
||||||
|
for(int i = 0; i<k-1; i++) {
|
||||||
|
if(to!=null) {
|
||||||
|
to = to.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(to == null) {
|
||||||
|
// reach the end
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// reverse nodes in [frm, to]
|
||||||
|
ListNode cur = frm.next;
|
||||||
|
for(int i = 0; i<k-1; i++) {
|
||||||
|
frm.next = cur.next;
|
||||||
|
cur.next = prev.next;
|
||||||
|
prev.next = cur;
|
||||||
|
cur = frm.next;
|
||||||
|
}
|
||||||
|
prev = frm;
|
||||||
|
}
|
||||||
|
return dummy.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
@@ -26,15 +26,18 @@ class Solution {
|
|||||||
public ListNode reverseBetween(ListNode head, int left, int right) {
|
public ListNode reverseBetween(ListNode head, int left, int right) {
|
||||||
ListNode dummy = new ListNode(0);
|
ListNode dummy = new ListNode(0);
|
||||||
dummy.next = head;
|
dummy.next = head;
|
||||||
ListNode prev = dummy;
|
ListNode prev, frm;
|
||||||
for(int i = 0; i< left-1; i++) {
|
prev = frm = dummy;
|
||||||
|
for(int i = 0; i<left-1; i++) {
|
||||||
prev = prev.next;
|
prev = prev.next;
|
||||||
}
|
}
|
||||||
ListNode cur = prev.next;
|
for(int i = 0; i < left; i++) {
|
||||||
ListNode tail = cur, tmp;
|
frm = frm.next;
|
||||||
for(int i = 0; i < right-left; i++) {
|
}
|
||||||
cur = tail.next;
|
ListNode cur;
|
||||||
tail.next = cur.next;
|
for(int i = 0; i<right-left; i++) {
|
||||||
|
cur = frm.next;
|
||||||
|
frm.next = cur.next;
|
||||||
cur.next = prev.next;
|
cur.next = prev.next;
|
||||||
prev.next = cur;
|
prev.next = cur;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user