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?

 # 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
"""
dummy = ListNode(-1)
dummy.next = head
cur, slow = dummy, dummy
while n > 0:
cur = cur.next
n -= 1
while cur.next is not None:
slow = slow.next
cur = cur.next
slow.next = slow.next.next
return dummy.next

[LC] 19. Remove Nth Node From End of List的更多相关文章

  1. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

  2. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  3. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  4. 【LeetCode】19. Remove Nth Node From End of List (2 solutions)

    Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...

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

  6. 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, ...

  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. Nginx和php交互的两种方式

    Unix socket 也叫IPC socket  也就是进程间通信套接字用于同一台主机上的不同进程间交换数据 TCP socket IP socket要利用主机的传输层(tcp),可以用于同一台主机 ...

  2. one_day_one_linuxCmd---tar命令

    <坚持每天学习一个 linux 命令,今天我们来学习 tar 命令> 摘要:tar 命令是一个 Linux 下的打包程序,通常在 Linux 下,打包和压缩是不同的程序,打包通过 tar ...

  3. java将HSSFWorkbook生成的excel压缩到zip中

    思路:1.写入输入流中. 2.将输入流加到ZipOutputStream压缩流中 List<DocumentModel> list = null; try { list = documen ...

  4. [极客大挑战 2019]PHP

    0x00知识点 1:直接扫描目录得到网站源码. 2:public.protected与private在序列化时的区别 protected 声明的字段为保护字段,在所声明的类和该类的子类中可见,但在该类 ...

  5. javaweb06 文件的下载

    1. 如何修改小工具或框架的源代码 ? 1). 原则: 能不修改就不修改. 2). 修改的方法: > 修改源代码, 替换 jar 包中对应的 class 文件. > 在本地新建相同的包, ...

  6. pandas读取和写入excel多个sheet表单

    一.读取单个表单 import pandas as pd excel_reader=pd.ExcelFile('文件.xlsx') # 指定文件 sheet_names = excel_reader. ...

  7. Python 安装zbar-py时出现 无法打开包括文件: “unistd.h” no such file or directory

    问题 途中使用的命令是cl.exe,在执行命令的时候找不到对应的unistd.h文件. unistd.h是Unix系统的文件,因此,十有八九,使用的是Windows系统.下面的代码可以修复,但是如果修 ...

  8. #JS# 如何判断一个字符串是否为日期格式

    var data = “2018-12-09”; //返回为false则是日期格式;isNaN(data)排除data为纯数字的情况(此处不考虑只有年份的日期,如‘2018’) if(isNaN(da ...

  9. 可视化---seaborn

    变量说明 x,y,hue 数据集变量 变量名 date 数据集 数据集名 row,col 更多分类变量进行平铺显示 变量名 col_wrap 每行的最高平铺数 整数 estimator 在每个分类中进 ...

  10. Django框架(五):模型(一) 定义属性

    1. 定义属性 Django根据属性的类型确定以下信息: 当前选择的数据库支持字段的类型 渲染管理表单时使用的默认html控件 在管理站点最低限度的验证 django会为表创建自动增长的主键列,每个模 ...