实现删除链表倒数第N个节点的功能
根据LeetCode19题的要求,实现了删除链表倒数第N个节点的功能。通过两个指针分别定位到第N个节点和链表头,当第一个指针移动到链表末尾时,第二个指针将跳过需要删除的节点,从而实现删除操作。提供了两个实现版本,分别在类Solution和类So中。
This commit is contained in:
parent
3aaee55251
commit
f6d0c4fc63
@ -0,0 +1,56 @@
|
||||
package cn.whaifree.redo.redo_all_240721;
|
||||
|
||||
import cn.whaifree.leetCode.model.ListNode;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/8/9 21:20
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode19 {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
// new Solution().removeNthFromEnd(ListNode.listNodeFromArray(new int[]{1, 2, 3, 4, 5}), 2).printList();
|
||||
new So().ListNoderemoveNthEromEnd(ListNode.listNodeFromArray(new int[]{1}), 1).printList();
|
||||
}
|
||||
|
||||
|
||||
class Solution {
|
||||
public ListNode removeNthFromEnd(ListNode head, int n) {
|
||||
|
||||
ListNode pre = new ListNode(-1, head);
|
||||
ListNode fast = pre;
|
||||
ListNode slow = pre;
|
||||
for (int i = 0; i < n; i++) {
|
||||
fast = fast.next;
|
||||
}
|
||||
|
||||
while (fast.next != null) {
|
||||
fast = fast.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
slow.next = slow.next.next;
|
||||
return pre.next;
|
||||
}
|
||||
}
|
||||
|
||||
class So {
|
||||
public ListNode ListNoderemoveNthEromEnd(ListNode head, int n) {
|
||||
ListNode first = new ListNode(0, head);
|
||||
ListNode second = head;
|
||||
for (int i = 0; i < n; i++) {
|
||||
second = second.next;
|
||||
}
|
||||
while (second != null) {
|
||||
first = first.next;
|
||||
second = second.next;
|
||||
}
|
||||
first.next = first.next.next;
|
||||
return head;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user