[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 ...
随机推荐
- android和Vitamio使用比较
在开始接触udp组播的时候先使用的Vitamio,播放时候声音卡顿 画面也会出现卡顿,后来又使用了VLC,画面挺好,,但是声音卡顿.最后不断测试发现是由于设备底层驱动处理视频部分有问题,导致程序播出的 ...
- BZOJ 4300 绝世好题(位运算)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4300 [题目大意] 给出一个序列a,求一个子序列b,使得&和不为0 [题解] ...
- passwordauthentication yes
ssh ip disconnected:no supported authentication methods available(server sent:publickey,gssapi-keyex ...
- xmu1125 越野车大赛(三分)
1125: 越野车大赛 Time Limit: 500 MS Memory Limit: 64 MB Special JudgeSubmit: 4 Solved: 3[Submit][Statu ...
- C# 动态载入Dll
1.新建測试dll及方法,用vs2010新建winform程序,详细代码例如以下: using System; using System.Collections.Generic; using Syst ...
- String、StringBuffer和StringBuild的区别
public class Test1 { public static void stringReplace (String text) { text = text.replace('j','i') ; ...
- Google Maps 学习笔记(三)
1.GPolyline折线对象和GPolygon多边形对象 html标签中必须包含v="urn:schemas-microsoft--com:vml"的命名空间 <html ...
- Windows API中几个函数的总结
[DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr ...
- SQL Server索引进阶:第二级,深入非聚集索引
原文地址: Stairway to SQL Server Indexes: Level 2, Deeper into Nonclustered Indexes 本文是SQL Server索引进阶系列( ...
- 简单天气应用开发——自定义TableView
顺利解析JSON数据后,天气数据已经可以随意提取了,现在要做的就是建立一个简单的UI. 实况信息较为简单,几个Lable就可以解决.主要是七天天气预报有点麻烦,那是一个由七个字典构成的数组,需要提取出 ...