题目

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个

Hints

Related Topics: LinkedList, Two Pointers

通过两个指针 后一个指针在前一个指针移动 n-1 个之后再移动 就能保证该指针是要移除的那个了

代码

Java

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode start = new ListNode(0);
ListNode slow = start, fast = start;
slow.next = head; for(int i=0;i<=n;i++){
fast = fast.next;
} while(fast!=null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return start.next;
}
}

Python

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
t = ListNode(head.val)
t.next = head
a = b = c = t
count = 0
while a.next!=None:
a = a.next
count += 1
if count>=n:
c = b
b = b.next
c.next = b.next
head = t.next
return head

蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]的更多相关文章

  1. [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 ...

  2. [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, ...

  3. 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 ...

  4. 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 ...

  5. (链表 双指针) leetcode 19. Remove Nth Node From End of List

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

  6. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

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

  7. [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 ...

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

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

  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. 20155213 《网络攻防》 Exp1 PC平台逆向破解

    20155213 <网络攻防> Exp1 PC平台逆向破解(5)M 实践内容 通过对实践对象--pwn20155213的linux可执行文件的修改或输入,完成以下三块: 手工修改可执行文件 ...

  2. 洛谷 1938 [USACO09NOV]找工就业Job Hunt

    洛谷 1938  [USACO09NOV]找工就业Job Hunt 题目描述 Bessie is running out of money and is searching for jobs. Far ...

  3. Ubuntu14.04 64位机上安装cuda8.0+cudnn5.0操作步骤

    查看Ubuntu14.04 64位上显卡信息,执行: lspci | grep -i vga lspci -v -s 01:00.0 nvidia-smi 第一条此命令可以显示一些显卡的相关信息:如果 ...

  4. loj#121.「离线可过」动态图连通性

    题面 话说#122怎么做啊 题解 我的\(\mathrm{LCT}\)水平极差,连最小生成树都快忘了,赶紧复习一下 做法和这篇是一样的 这道题还可以练习线段树分治 还可以练习ETT 果然是道吼题 代码 ...

  5. CF 1110 E. Magic Stones

    E. Magic Stones 链接 题意: 给定两个数组,每次可以对一个数组选一个位置i($2 \leq i \leq n - 1$),让a[i]=a[i-1]+a[i+1]-a[i],或者b[i] ...

  6. SQL Server 跨库查询

    1. 开启Ad Hoc Distributed Queries组件,在sql查询编辑器中执行如下语句: reconfigure reconfigure 2. 跨库查询操作 select * from ...

  7. Form,选择并转移导航菜单

    1.代码实例 <!DOCTYPE html> <html> <head> <title>选择并转移导航菜单</title> <meta ...

  8. 《数据结构与算法图解》 分享 pdf下载

    链接:https://pan.baidu.com/s/1gOMlwU5ucHYDVazvVMk2uw提取码:bk5x

  9. happybase(TSocket read 0 bytes)

    关于报错happybase 是使用python连接hbase的一个第三方库,目前基于thrift1 .在使用过程中经常碰到报错 TTransportException(type=4, message= ...

  10. IEEE1588 ( PTP ) 协议简介

    IEEE1588 协议,又称 PTP( precise time protocol,精确时间协议),可以达到亚微秒级别时间同步精度,于 2002 年发布 version 1,2008 年发布 vers ...