19. Remove Nth Node From End of List(C++,Python)
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: ->->->->, and n = . After removing the second node from the end, the linked list becomes ->->->.
Note:
Given n will always be valid.
Try to do this in one pass.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
struct ListNode* cur;
struct ListNode* pre;
cur = head;
pre = head;
if(head == NULL)
return head;
while(n)
{
cur = cur->next;
n--;
}
if(cur == NULL){
head = head->next;
return head;
}
while(cur->next != NULL){
cur = cur->next;
pre = pre->next;
}
pre->next = pre->next->next;
return head; }
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
"""
#这里的头结点是第一个结点
#判断链表是否为空
if head == None:
return head #设置两个标志位
cur = head
pre = head #cur先走n步
while n:
cur = cur.next
n -= 1 #如果cur为空,则说明n为链表长度
if cur == None:
return head.next #pre标志位和cur一起走,直到cur走到最后一个结点
while cur.next != None:
cur = cur.next
pre = pre.next
#删除倒数第n个结点
pre.next = pre.next.next
return head
19. Remove Nth Node From End of List(C++,Python)的更多相关文章
- 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. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【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 ...
- [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 ...
- 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 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, ...
- 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 ...
- 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 ...
随机推荐
- varnish安装
安装pcrevarnish 依赖pcre进行url正则匹配. cd pcre-8.12./configure --prefix=/usr/local/make&&make instal ...
- Qt中显示图像的两种方法
博客转载自:https://blog.csdn.net/lg1259156776/article/details/52325361 在Qt中处理图片一般都要用到QImage类,但是QImage的对象不 ...
- vray学习笔记(1)vray介绍
vray是个什么东西? 它是个渲染器. 渲染器是个什么东西? 渲染器就是3d软件里面把模型画成一张图片的东西,渲染的过程就是把3D物体变成2D画面的过程. 模型是个什么东西? 模型就是模型,它由两部分 ...
- js实现上传单个文件
js上传文件:js 上传单个文件(任意大小) 疯狂代码 http://www.CrazyCoder.cn/ :http:/www.CrazyCoder.cn/Javascript/Article832 ...
- Luogu 3168 [CQOI2015]任务查询系统
区间修改单点查询,又观察到是一个k小,考虑主席树上做差分 一开始样例疯狂挂,后来发现主席树在一个历史版本上只能修改一次,所以要开2*n个根结点,记录一下每个时间对应的根结点编号 然后80分,考虑到当一 ...
- WordCount程序及测试
Github地址:https://github.com/CG0317/WordCount PSP表: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划 30 ...
- Socket编程--基础(基本server/client实现)
IPv4套接口地址结构 IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件中 LINUX结构下的常用结构,一般创建套接字的时候都要将这个结构里面的 ...
- code manager tools myeclipse10 svn插件安装及使用
一.下载: 1.在线安装地址:http://subclipse.tigris.org/update_1.6.x 2.安装包下载地址:http://subclipse.tigris.org/servle ...
- GoogLeNet InceptionV2/V3/V4
仅用作自己学习 这篇文章中我们会详细讲到Inception V2/V3/V4的发展历程以及它们的网络结构和亮点. GoogLeNet Inception V2 GoogLeNet Inc ...
- 安装eclipse(tomcat配置maven搭建){Java基础之开发工具}
安装eclipse 1.下载eclipse-jee-neon-3-win32-x86_64 zip 百度云盘 解压 2. 下载JDK 官网 JDK 8 JDK电脑上环境变量配置 Path路径 % ...