题解 | #删除链表的倒数第n个节点#TOP9

思路:
1.首先当只有一个节点时,n=1 ,那删除后就是空节点
2.还有当n的值刚好是头节点,例如倒数第3个节点,而节点长度为3,这种就是返回head.next了
3.其他情况,我们将slow指向到 要删除节点的前一个节点,那怎么才能指到呢?我们用fast节点先指向头节点,然后偏移 n - 1 个节点位置,此时[slow,fast]长度为n,如果fast节点后是空节点,那么就是第2中情况了,否则fast = fast.next,然后[slow,fast]长度为n+1,循环遍历,知道fast节点的next为空,然后slow.next = slow.next.next就删除了


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

        return head;
    }
}
面试必刷TOP101 文章被收录于专栏

面试必刷TOP101

全部评论

相关推荐

07-09 12:12
门头沟学院 Java
5月底投简历7月初开奖收获秋招第一个offer,虽然白菜价,但至少能保底了
土木转行ing:土木博士想转图像,最后拿了 tp 提前批 sp 最低档,感觉性价比不高
TP-LINK开奖132人在聊
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务