LeetCode Top 100 Liked Questions 19. Remove Nth Node From End of List (Java版; Medium)

welcome to my blog

LeetCode Top 100 Liked Questions 19. Remove Nth Node From End of List (Java版; Medium)

题目描述

Given a linked list, remove the n-th node from the end of list and return its head.

Example:

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:

Given n will always be valid.

Follow up:

Could you do this in one pass?
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next=head;
        ListNode pre=dummy, cur=head, right=head;
        for(int i=0; i<n-1; i++){
            right = right.next;
        }
        while(right.next!=null){
            pre = cur;
            cur = cur.next;
            right = right.next;
        }
        pre.next = cur.next;
        return dummy.next;
    }
}
第一次做, 一次遍历即可, 题目的基础就是找到链表中倒数第n个节点; 注意:不要让fast走到null,这样的话fast和slow得相隔k个节点,由于slow和fast的起点是head,所以相隔k个节点的话需要链表中有k+1个节点, 这样判断的话就麻烦了,比如链表只有k个节点,检查长度时fast走到了null,此时不能返回null,需要配合k进行判断,就变麻烦了。所以一步到位,让fast和slow相隔k-1个节点,不满足就说明链表长度小于k,返回null; 这道题的initialize, execute, decision不是传统模式
  • 之所以让fast走到最后一个节点而不是走到null, 是为了避免出现这样的情况: fast走到null时, 是正好走到null还是因为链表长度不够k? 出现了歧义
/*
这倒不一定先创建一个tempHead,因为处理头结点和非头结点不麻烦
要记得,删除某个节点的话, 得保存前一个节点
*/
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null)
            return null;
        //initialize
        ListNode pre=null, left=head, right = head;
        for(int i=0; i<n-1; i++){
            //execute
            right = right.next;
            //decision
            if(right==null)
                return null;
        }
        //循环终止条件也可以用right!=null,但要改动一下上面的for循环次数
        while(right.next!=null){
            //save last node
            pre = left;
            //update
            left = left.next;
            right = right.next;
        }
        //这里可以按照left是否等于head进行分类; 也可以按照pre是否为null进行分类; 二者是等价的
        if(left==head)
            return head.next;
        pre.next = left.next;
        return head;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值