doc: 2025-10-24打卡
This commit is contained in:
52
143.reorder-list.java
Normal file
52
143.reorder-list.java
Normal 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
|
||||
|
||||
42
147.insertion-sort-list.java
Normal file
42
147.insertion-sort-list.java
Normal 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
|
||||
|
||||
51
19.remove-nth-node-from-end-of-list.java
Normal file
51
19.remove-nth-node-from-end-of-list.java
Normal 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
|
||||
|
||||
37
24.swap-nodes-in-pairs.java
Normal file
37
24.swap-nodes-in-pairs.java
Normal 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
|
||||
|
||||
45
92.reverse-linked-list-ii.java
Normal file
45
92.reverse-linked-list-ii.java
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user