算法-leetcode-链表问题- 19. 删除链表的倒数第 N 个结点
文章目录
15, 19. 删除链表的倒数第 N 个结点
思路1: 暴力遍历删除
思路2: 使用栈结构
思路3: 双指针法
- 第一个指针指向第0个节点, 第二个指针指向n个节点,
- 两个指针同时往有移动,
- 当第二个指针指向最后一个节点当时候, 第一个指针必定指向倒数第n个节点的前一个节点
package com.shangguigu.dachang.algrithm.A05_linkedList;
import java.util.Stack;
public class A51_removeNthFromEnd {
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(3);
ListNode l4 = new ListNode(4);
ListNode l5 = new ListNode(5);
l1.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
ListNode listNode = removeNthFromEnd_by_twoPointer(l1, 2);
System.out.println("结果是:" + listNode);
}
public static ListNode removeNthFromEnd_by_twoPointer(ListNode head, int n){
ListNode first = head;
ListNode second = head;
for (int i = 0; i < n; i++) {
if (i < n - 1 && first.next == null) {
return head;
} else if (i == n - 1 && first.next == null) {
return head.next;
} else {
first = first.next;
}
}
while (first.next != null) {
first = first.next;
second = second.next;
}
second.next = second.next.next;
return head;
}
public static ListNode removeNthFromEnd_by_stack(ListNode head, int n){
Stack<ListNode> stack = new Stack<>();
ListNode currNode = head;
int length = 0;
while (currNode != null) {
length++;
stack.push(currNode);
currNode = currNode.next;
}
if (n > length) {
return head;
} else if (n == length) {
return head.next;
} else {
for (int i = 0; i <= n-1; i++) {
stack.pop();
}
stack.peek().next = stack.peek().next.next;
return stack.firstElement();
}
}
public static ListNode removeNthFromEnd_by_violance(ListNode head, int n) {
int length = 0;
ListNode theNode1 = head;
while (theNode1 != null) {
length++;
theNode1 = theNode1.next;
}
if (n > length) {
return head;
} else if (n == length) {
head = head.next;
return head;
} else {
ListNode theNode = head;
ListNode preNode = head;
for (int i = 0; i < length - n; i++) {
preNode = theNode;
theNode = theNode.next;
}
preNode.next = theNode.next;
return head;
}
}
}