doc: 2025-10-24打卡

This commit is contained in:
wu xiangkai
2025-10-24 16:32:48 +08:00
parent 0540e83113
commit 6dc597fa75
5 changed files with 227 additions and 0 deletions

52
143.reorder-list.java Normal file
View File

@@ -0,0 +1,52 @@
/*
* @lc app=leetcode id=143 lang=java
*
* [143] Reorder List
*/
// @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 void reorderList(ListNode head) {
if(head == null || head.next == null) {
return;
}
// reverse the right side and merge
ListNode d1 = new ListNode(0);
d1.next = head;
ListNode slow = d1, fast = d1;
while(fast!=null && fast.next!=null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode rh = slow.next, lh = head;
slow.next = null;
// reverse the right half
ListNode prev = null, cur = rh, t;
while(cur != null) {
t = cur.next;
cur.next = prev;
prev = cur;
cur = t;
}
rh = prev;
// merge the two linkedlist
while(rh != null) {
t = lh.next;
lh.next = rh;
lh = rh;
rh = t;
}
}
}
// @lc code=end

View File

@@ -0,0 +1,42 @@
/*
* @lc app=leetcode id=147 lang=java
*
* [147] Insertion Sort List
*/
// @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 insertionSortList(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = null;
ListNode t;
while(head != null) {
t = head;
head = head.next;
ListNode prev = dummy;
while(true) {
if(prev.next == null || prev.next.val > t.val) {
t.next = prev.next;
prev.next = t;
break;
} else {
prev = prev.next;
}
}
}
return dummy.next;
}
}
// @lc code=end

View File

@@ -0,0 +1,51 @@
/*
* @lc app=leetcode id=19 lang=java
*
* [19] Remove Nth Node From End of List
*/
// @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 {
/**
* 在删除链表的倒数第n个node时如果想在一次遍历中删除可以使用两个指针slow fast
* 其中fast指针领先slow指针n个位置之后fast指针和slow指针以相同的pace向后移动
* 当fast到达链表尾部时slow则是倒数第n个位置
* 可以通过prev记录slow的前一个位置然后删除prev节点
*
* @param head
* @param n
* @return
*/
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null) {
return null;
}
ListNode prev = null, slow = head, fast = head;
for(int i = 0; i < n; i++) {
fast = fast.next;
}
while(fast != null) {
fast = fast.next;
prev = slow;
slow = slow.next;
}
if(prev == null) {
head = head.next;
} else {
prev.next = slow.next;
}
return head;
}
}
// @lc code=end

View File

@@ -0,0 +1,37 @@
/*
* @lc app=leetcode id=24 lang=java
*
* [24] Swap Nodes in Pairs
*/
// @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 swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy, cur = head, next = head == null ? null : head.next, t;
while(cur !=null && next != null) {
prev.next = next;
t = next.next;
next.next = cur;
cur.next = t;
prev = cur;
cur = cur.next;
next = cur == null ? null : cur.next;
}
return dummy.next;
}
}
// @lc code=end

View File

@@ -0,0 +1,45 @@
/*
* @lc app=leetcode id=92 lang=java
*
* [92] Reverse Linked List II
*/
// @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 {
/**
* 链表逆序:头插法
* @param head
* @param left
* @param right
* @return
*/
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
for(int i = 0; i< left-1; i++) {
prev = prev.next;
}
ListNode cur = prev.next;
ListNode tail = cur, tmp;
for(int i = 0; i < right-left; i++) {
cur = tail.next;
tail.next = cur.next;
cur.next = prev.next;
prev.next = cur;
}
return dummy.next;
}
}
// @lc code=end