package com.daily.daily20210204;
import java.util.List;
/**
* @Project: dailyCode
* @Site: http://www.zhao1iang.club/
* @Copyright: ©CodeLamp
* @Author: zhaoliang
* @Create: 2021-02-04 15:16
* @Desc: 删除链表的倒数第n个节点
**/
public class removeNthFromEnd {
class ListNode{
int val;
ListNode next;
ListNode(int x){
this.val = x;
}
}
public ListNode removeNthFromEnd(ListNode node,int N){
if (node==null)return null;
ListNode fast = node;
ListNode last = node;
while(N-- >0){
fast = fast.next;
}
if (fast == null) return node.next;
while (fast.next!=null){
fast = fast.next;
last = last.next;
}
last.next = last.next.next;
return node;
}
}
日常刷题-删除链表的倒数第n个节点
最新推荐文章于 2024-07-21 22:27:18 发布