给定一个链表,删除链表的倒数第 n 个节点并返回链表的头指针
例如,
例如,
给出的链表为:
,
.
删除了链表的倒数第
个节点之后,链表变为
.
删除了链表的倒数第
数据范围: 链表长度
,链表中任意节点的值满足
要求:空间复杂度
,时间复杂度 )
备注:
备注:
题目保证
一定是有效的
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
if (head == null) return null;
ListNode pre = head;
ListNode cur = head;
//先跳过n个
for (int i = 0; i < n; i++) {
cur = cur.next;
}
//如果当前为空,则删除第一个
if (cur == null) {
return head.next;
}
//跳过一个,使pre.next为倒数第n个
cur = cur.next;
while (cur != null) {
pre = pre.next;
cur = cur.next;
}
//删除对应节点
pre.next = pre.next.next;
return head;
}
} import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode newhead = new ListNode(0);
newhead.next = head;
ListNode fast = newhead;
ListNode slow = newhead;
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 newhead.next;
}
} public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
if (head == null){
return null;
}
// write code here
ListNode current = head;
int i =1;
while (current != null){
current = current.next;
i++;
}
if (n > i-1){
return null;
}
if (n == i-1){
head = head.next;
return head;
}
ListNode current1 = head;
for (int j = 1; j< i-1-n; j++){
current1 = current1.next;
}
current1.next = current1.next.next;
return head;
} 有三种情况,分别考虑就好了
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
* 1.先获取链表长度,判断n 与长度的关系,如果n >5 ,则返回null
* 2.通过倒数第n个节点,找出正数第 length-n+1 个节点
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
if (head == null || n == 0) {
return null;
}
int length = 0;
ListNode cur = head;
while (cur != null) {
length++;
cur = cur.next;
}
if (n > length) {
return null;
}
int deleteNode = length - n + 1;
cur = head;
if (n == length) {
return head.next;
}
int i = 2;
while (cur.next != null) {
if (i == deleteNode) {
cur.next = cur.next.next;
break;
} else {
cur = cur.next;
}
i++;
}
return head;
}
} public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
if(head == null){
return null;
}
ListNode slow = head;
ListNode fast = head;
for(int i=0; i<n; i++){
fast = fast.next;
}
if(fast == null){
return head.next;
}
while(fast.next != null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return head;
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode fast = head;
// 慢指针用于标记被删除点
ListNode slow = head;
// 被删除点的前一个节点
ListNode pre = new ListNode(-1);
// 指向头结点
pre.next = head;
// 利用快慢指针找到被删除点的位置
for(int i = 0;i<n;i++){
fast = fast.next;
}
while(fast!= null){
fast = fast.next;
slow = slow.next;
pre = pre.next;
}
// slow == head表示被删除点为头节点,删除头节点后,新的头节点为原头结点的下一节点
if(slow == head) return head.next;
// 被删除点不是头结点,则将被删除点的前一节点指向被删除点的下一节点,完成指定节点的删除
pre.next = slow.next;
return head;
}
} public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode nextNode = head;
List<ListNode> list = new ArrayList<>();
while(nextNode != null){
list.add(nextNode);
nextNode = nextNode.next;
}
int len = list.size();
if(n == len){
head = head.next;
}else if(n == 1){
list.get(len - 2).next = null;
}else{
list.get(len - n - 1).next = list.get(len - n + 1);
}
return head;
} public ListNode removeNthFromEnd (ListNode head, int n) {
if(head==null) return null;
ListNode dummy=new ListNode(-1);
dummy.next=head;
ListNode fast=dummy;//fast从dummy开始走
while(n-->0){
fast=fast.next;
}
ListNode slow=dummy;//slow从dummy开始走
while(fast.next!=null){
fast=fast.next;
slow=slow.next;
}
ListNode tmp=slow.next.next;
slow.next=tmp;
return dummy.next;
} import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
//判断 head == null
if (head == null) {
return null;
}
//找到倒数第 n + 1个节点
ListNode fast = head;
ListNode slow = head;
while (n-- > 0 && fast != null) {
fast = fast.next;
}
if (fast == null) {
return head.next;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode vHead = new ListNode(-1);
vHead.next = head;
int length = 0;
for(ListNode p = head; p != null; p = p.next){
length ++;
}
ListNode pre = vHead;
ListNode cur = head;
for(int i = 0; i < length - n; i++){
pre = pre.next;
cur = cur.next;
}
pre.next = cur.next;
cur = null;
return vHead.next;
} public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
if(head.next == null && n == 1) return null;
ListNode dummy = new ListNode(0) ,pre = head , cur = head , temp = dummy;
dummy.next = head;
//先让右指针走n步,当右指针为null的时候说明没有倒数第n个节点
for (int i = 0; i < n; i++) {
if(cur == null) return head;
cur = cur.next;
}
//找到要删除的节点
while(cur != null){
cur = cur.next;
temp = temp.next;
pre = pre.next;
}
//进行删除
temp.next = pre.next;
return dummy.next;
} import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
if (n == 0) {
return head;
}
// write code here
int length = 0;
ListNode temp = head;
while (temp != null) {
temp = temp.next;
length++;
}
if (length == n) {
return head.next;
}
// n一定是有效的,只需移动到倒数第n个元素之前即可
int counter = 0;
temp = head;
while (temp != null) {
if (length - counter == n + 1) {
// 找到了倒数第n个元素的前一个元素
temp.next = temp.next.next;
return head;
}
temp = temp.next;
counter++;
}
return head;
}
}