题目描述:Remove Nth Node From End of List

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

For 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.
Try to do this in one pass.

分析:

题目中说n是合法的,就不用对n进行检查了。用标尺的思想,两个指针相距为n-1,后一个到表尾,则前一个到n了。

① 指针p、q指向链表头部;

② 移动q,使p和q差n-1;

③ 同时移动p和q,使q到表尾;

④ 删除p。

(p为second,q为first)

代码如下:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) { if(head == NULL || head->next == NULL) return NULL; ListNode * first = head;
ListNode * second = head; for(int i = 0;i < n;i++)
first = first->next; if(first == NULL)
return head->next; while(first->next != NULL){
first = first->next;
second = second->next;
} second->next = second->next->next;
return head;
}
};

Java:

    public ListNode removeNthFromEnd(ListNode head, int n) {

        if (head == null || head.next == null) {
return null;
} ListNode first = head;
ListNode second = head; for (int i = 0; i < n; i++) {
first = first.next;
} if (first == null) {
return head.next;
} while (first.next != null) {
first = first.next;
second = second.next;
} second.next = second.next.next;
return head;
}

LeetCode 019 Remove Nth Node From End of List的更多相关文章

  1. 【JAVA、C++】LeetCode 019 Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  2. [Leetcode][019] Remove Nth Node From End of List (Java)

    题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointer ...

  3. 【LeetCode】019. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  4. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  5. 【leetcode】Remove Nth Node From End of List

    题目简述: Given a linked list, remove the nth node from the end of list and return its head. For example ...

  6. 【leetcode】Remove Nth Node From End of List(easy)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  7. [leetcode 19] Remove Nth Node From End of List

    1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  8. Java [leetcode 19]Remove Nth Node From End of List

    题目描述: Given a linked list, remove the nth node from the end of list and return its head. For example ...

  9. Leetcode 19——Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

随机推荐

  1. 循序渐进VUE+Element 前端应用开发(25)--- 各种界面组件的使用(1)

    在我们使用Vue+Element开发前端的时候,往往涉及到很多界面组件的使用,其中很多直接采用Element官方的案例即可,有些则是在这个基础上封装更好利用.更少代码的组件:另外有些则是直接采用第三方 ...

  2. 数据库本地data source

    数据源表示数据库所在设备的ip地址或名称,基本上有以下几种写方法 data source = (local) data source = "127.0.0.1" data sour ...

  3. ZOJ 1005 Jugs(BFS)

    Jugs In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with th ...

  4. 力扣 - 142. 环形链表 II

    目录 题目 思路1 代码实现 思路2 代码实现 题目 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链 ...

  5. Ubuntu 16.04升级18.04

    原文:https://blog.csdn.net/sean_8180/article/details/81075659 1.更新资源$ sudo apt-get update$ sudo apt-ge ...

  6. 剑指29:最小的k个数

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. class Solution {public:    vector& ...

  7. Centos 7挂载本地ISO光盘

    在Linux系统上挂载系统镜像作为yum本地仓库或者安装某个常用RPM包. mount语法: mount -t 类型 -o 挂接方式 源路径 目标路径 -t 选项: iso9660:光盘或光盘镜像 m ...

  8. Flink基础:时间和水印

    ​ 往期推荐: Flink基础:入门介绍 Flink基础:DataStream API Flink基础:实时处理管道与ETL Flink深入浅出:资源管理 Flink深入浅出:部署模式 Flink深入 ...

  9. 对udp dns的思考2

    上一篇文章写道了udp 使用reuseport 多线程编程!!! 但是有几个问题需要考虑一下: 之前hash使用sip sport dip dport为key, 很正常同一个客户端回hash到同一个s ...

  10. ceph的pg平衡插件balancer

    前言 ceph比较老的版本使用的reweight或者osd weight来调整平衡的,本篇介绍的是ceph新的自带的插件balancer的使用,官网有比较详细的操作手册可以查询 使用方法 查询插件的开 ...