From 6dc597fa7595d39a82b640da0f710f703409aed4 Mon Sep 17 00:00:00 2001 From: wu xiangkai Date: Fri, 24 Oct 2025 16:32:48 +0800 Subject: [PATCH] =?UTF-8?q?doc:=202025-10-24=E6=89=93=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 143.reorder-list.java | 52 ++++++++++++++++++++++++ 147.insertion-sort-list.java | 42 +++++++++++++++++++ 19.remove-nth-node-from-end-of-list.java | 51 +++++++++++++++++++++++ 24.swap-nodes-in-pairs.java | 37 +++++++++++++++++ 92.reverse-linked-list-ii.java | 45 ++++++++++++++++++++ 5 files changed, 227 insertions(+) create mode 100644 143.reorder-list.java create mode 100644 147.insertion-sort-list.java create mode 100644 19.remove-nth-node-from-end-of-list.java create mode 100644 24.swap-nodes-in-pairs.java create mode 100644 92.reverse-linked-list-ii.java diff --git a/143.reorder-list.java b/143.reorder-list.java new file mode 100644 index 0000000..eeaefd4 --- /dev/null +++ b/143.reorder-list.java @@ -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 + diff --git a/147.insertion-sort-list.java b/147.insertion-sort-list.java new file mode 100644 index 0000000..7535b46 --- /dev/null +++ b/147.insertion-sort-list.java @@ -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 + diff --git a/19.remove-nth-node-from-end-of-list.java b/19.remove-nth-node-from-end-of-list.java new file mode 100644 index 0000000..cbc9897 --- /dev/null +++ b/19.remove-nth-node-from-end-of-list.java @@ -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 + diff --git a/24.swap-nodes-in-pairs.java b/24.swap-nodes-in-pairs.java new file mode 100644 index 0000000..fb71ce6 --- /dev/null +++ b/24.swap-nodes-in-pairs.java @@ -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 + diff --git a/92.reverse-linked-list-ii.java b/92.reverse-linked-list-ii.java new file mode 100644 index 0000000..66fe080 --- /dev/null +++ b/92.reverse-linked-list-ii.java @@ -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 +