leetcode最火100题详解笔记——第 19 题(删除链表的倒数第N个结点)

leetcode最火100题详解笔记——第 19 题(删除链表的倒数第N个结点)


1. 题目

给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
进阶:你能尝试使用一趟扫描实现吗?

在这里插入图片描述

输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5]


示例 2:

输入:head = [1], n = 1 输出:[]


示例 3:

输入:head = [1,2], n = 1 输出:[1]


提示:

  • 链表中结点的数目为 sz
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz

2. 解题思路及算法


  • 思路: 由于我们需要找到倒数第 n 个节点,因此我们可以使用两个指针 first 和 second 同时对链表进行遍历,并且 first 比 second 超前 n 个节点。当 first 遍历到链表的末尾时,second 就恰好处于倒数第 n 个节点。

3. 核心部分参考代码

/**
 * 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 removeNthFromEnd(ListNode head, int n) {
        ListNode fast , slow;
        fast = slow = head;
        while( n -- > 0){
            fast = fast.next;
        }
        if(fast == null){
            return head.next;
        }
        
        while(fast != null && fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;

        return head;
    }
}

4. 测试参考完整代码


/**
	 * 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; }
	 * }
	 */

/*
 * 
 * 注意:题目中n不会超过链表的长度
 *
 * 
 * 
 */


public class RemoveNthFromEndTest_19{
	public static void main(String[] args){
		
		//构造测试样本的链表
		
		ListNode headTest = new ListNode(1);
		headTest.next = new ListNode(2);
		headTest.next.next = new ListNode(3);	
		headTest.next.next.next = new ListNode(4);
		headTest.next.next.next.next = new ListNode(5);
		System.out.println("样本链表,未删除任何节点时----");
		RemoveNthFromEnd_19.list(headTest);
		System.out.println("样本链表,删除选定节点时----");
		RemoveNthFromEnd_19.list(RemoveNthFromEnd_19.removeNthFromEnd(headTest,2));
		
	}
}


class RemoveNthFromEnd_19 {
	
    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode fast , slow;   //定义快慢指针
        fast = slow = head;
        
        //快指针先前进n步
        while( n -- > 0){
            fast = fast.next;
        }
        
        //情况一(特殊情况): 当快指针直接走到链表尾的情况,链表尾的指针next为null
        if(fast == null){
            return head.next;
        }
        
        
        
        //情况二:当快指针的位置在链表的第一个节点位置到倒数第二个位置之间时
        while(fast != null && fast.next != null){
        	//让快慢指针同步向前走
            fast = fast.next;
            slow = slow.next;
        }
        
        //slow.next就是倒数第n个节点,删除它即可满足题意
        slow.next = slow.next.next;

        return head;
    }
    
    

	
	public static void list(ListNode targetList){
		
		
		ListNode temp = targetList;
		
		while(true){
			//判断是否到链表最后
			if(temp == null){
				break;
			}
			//输出节点信息
			System.out.println(temp);
			//将temp后移,一定小心
			temp = temp.next;
						
		}
	
	}
	
   		
}




//定义链表节点
class ListNode{
	int val;
	ListNode next;
	public ListNode(int val){
		this.val = val;
	}	
	public ListNode(int val, ListNode next){ 
		this.val = val; this.next = next; 
	}
	
	
	//重写toSring方法用于测试样本输出显示
	@Override
	public String toString() {
		return "ListNode [val=" + val + "]";
	}
	
}

运行结果:

样本链表,未删除任何节点时----
ListNode [val=1]
ListNode [val=2]
ListNode [val=3]
ListNode [val=4]
ListNode [val=5]
样本链表,删除选定节点时----
ListNode [val=1]
ListNode [val=2]
ListNode [val=3]
ListNode [val=5]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值