[leetcode]Remove Nth Node From End of List @ Python
原题地址:http://oj.leetcode.com/problems/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个节点。
解题思路:加一个头结点dummy,并使用双指针p1和p2。p1先向前移动n个节点,然后p1和p2同时移动,当p1.next==None时,此时p2.next指的就是需要删除的节点。
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @return a ListNode
def removeNthFromEnd(self, head, n):
dummy=ListNode(0); dummy.next=head
p1=p2=dummy
for i in range(n): p1=p1.next
while p1.next:
p1=p1.next; p2=p2.next
p2.next=p2.next.next
return dummy.next
[leetcode]Remove Nth Node From End of List @ Python的更多相关文章
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- [LeetCode] 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 ...
- 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, Give ...
- 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, Give ...
- [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, Give ...
- [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点
Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...
- leetcode remove Nth Node from End python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
随机推荐
- hdu-4027线段树练习
title: hdu-4027线段树练习 date: 2018-10-10 18:07:11 tags: acm 算法 刷题 categories: ACM-线段树 # 概述 这道线段树的题可以说是我 ...
- python3.6 利用requests和正则表达式爬取猫眼电影TOP100
import requests from requests.exceptions import RequestException from multiprocessing import Pool im ...
- MyBatis 插入时返回刚插入记录的主键值
MyBatis 插入时返回刚插入记录的主键值 一.要求: 1.数据库表中的主键是自增长的,如:id: 2.获取刚刚插入的记录的id值: 二.源代码: 1.User.java package cn.co ...
- php 安全模式限制函数
表 42-2. 安全模式限制函数 函数名 限制 dbmopen() 检查被操作的文件或目录是否与正在执行的脚本有相同的 UID(所有者). dbase_open() 检查被操作的文件或目录是否与正在执 ...
- 解耦你的HTML,CSS和JAVASRIPT
注:本文为翻译文章,原文<Decoupling Your HTML, CSS, and JavaScript> 今天在web上任何大一点的网站或应用程序都包含大量的html,css和jav ...
- PHP self与static区别
this,static和self. self和this还是很好区分的,可是self和static就很糊涂了,两者都能调用静态的方法和属性,看似使用上没有什么太大的分别,但是实际上分别很大,先来看下面这 ...
- PIC JDM Prototype Programmer 1001
In need of a programmer for PIC micro controllers I decided to build my own one. This programmer has ...
- java基础学习总结——static关键字
一.static关键字
- MVC控制器传递多个Model到视图,使用ViewData, ViewBag, 部分视图, TempData, ViewModel, Tuple
从控制器传递多个Model到视图,可以通过ViewData, ViewBag, PartialView, TempData, ViewModel,Tuple等,本篇逐一体验.本篇源码在github. ...
- 使用Html.EditorFor()为文本框加上maxlength,placeholder等属性
当想通过Html.EditorFor()给文本框加上maxlength,placeholder等属性的时候,发现Html.EditorFor()没有提供可直接加上这些属性的重载方法,如何做到呢? □ ...