[LeetCode]题解(python):019-Remove Nth Node From End of List
题目来源:
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
题意分析:
这道题是给定一个链表,删除倒数第n个节点。提醒,1.输入的链表长度必然大于n,2.尽量通过访问一次就得到结果。
题目思路:
这道题的问题在于如何找到倒数第n个节点。由于只能访问一次,所以可以建立两个链表,tmp1和tmp2。tmp1先访问第一个到n个节点。这时候,tmp2从0开始访问。等tmp1访问结束的时候,tmp2刚好访问到倒数第n个节点。
代码(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
"""
ans = ListNode(0);
ans.next = head
tmp1 = ans
tmp2 = ans
i = 0
while i < n:
tmp1 = tmp1.next
i += 1
while tmp1.next:
tmp1 = tmp1.next
tmp2 = tmp2.next
tmp2.next = tmp2.next.next
return ans.next
转载请注明出处:http://www.cnblogs.com/chruny/p/4844007.html
[LeetCode]题解(python):019-Remove Nth Node From End of List的更多相关文章
- 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, ...
- LeetCode 019 Remove Nth Node From End of List
题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + 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 ...
- 【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 ...
- leetcode第19题--Remove Nth Node From End of List
Problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...
- 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 ex ...
- 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, Gi ...
- 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, G ...
- [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 ...
随机推荐
- Windows Azure 社区新闻综述(#73 版)
欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure的社区推动新闻.内容和对话. 以下是过去一周基于您的反馈汇集在一起的内容: 文章.视频和博客文章 · Windows A ...
- hihoCoder 1388 Periodic Signal(FFT)
[题目链接] http://hihocoder.com/problemset/problem/1388 [题目大意] 给出A数列和B数列,求下图式子: [题解] 我们将多项式拆开,我们可以得到固定项A ...
- oracle rac ha
ha,仅只是在操作系统层面进行数据库的监控和管理,一般只针对单实例数据库使用.优点是管理方便,应用开发方便(方便了开发商):工程投入较小.缺点是,具有所有单实例数据库的缺点:如:容错能力差,续航能力差 ...
- Hibernate 配置详解(12) 其实我也不想用这么土的名字
hibernate.hbm2ddl.import_files 这个配置用于在hibernate根据映射文件执行DDL之前,如果我们自己设置了要事先运行的SQL文件,hibernate就会先执行这些SQ ...
- Java图形化界面设计——布局管理器之GridLayout(网格布局)
网格布局特点: l 使容器中的各组件呈M行×N列的网格状分布. l 网格每列宽度相同,等于容器的宽度除以网格的列数. l 网格每行高度相同,等于容器的高度除以网格的行数. l 各组件的排列方式 ...
- java字符串函数及理解
Java中的字符串也是一连串的字符.但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java将字符串作为String类型对象来处理.将字符串作为内置的对象处理允许Java提供十分丰富的功能特性 ...
- svn在linux下的使用(svn命令行)ubuntu 删除 新增 添加 提交 状态查询 恢复
合并步骤:(1)先切换到分支:(2)svn merge trunk . (3)svn sw trunk (4)svn merge --reintegrate branch . svn merge ht ...
- XML 文档解析操作
sing System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security; ...
- BAE初试
BAE是百度的应用开发托管平台. 支持python nodejs java php 这几个环境~ 我在BAE上面搭建了1个wordpress. 记录下开启一个app的过程. 下面是所需工具 ---版 ...
- javascript 的工具方法 --- 类型判断
Javascript中常见类型对象有: Boolean, Number, String, Function, Array, Date, RegExp, Object, Error, Symbol等等. ...