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)的更多相关文章

  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. Group Layout

    ----------------siwuxie095                             将根面板 contentPane 的布局切换为 Group Layout     Grou ...

  2. mysql 5.7.11 源码安装

    mysql5.711安装 1.安装boost包下载地址http://sourceforge.net/projects/boost/files/boost/ 2.解压boost_1_59_0.tar.g ...

  3. 283E&EZOJ #89 Cow Tennis Tournament

    传送门 分析 我们考虑用所有的情况减去不合法的情况 不难想出所有情况为$C_n^3$ 于是我们考虑不合法的情况 我们知道对于一个不合法的三元组$(a,b,c)$一定是修改后$a<b,b>c ...

  4. Paxos在大型系统中的应用场景

    https://timyang.net/distributed/paxos-scenarios/ 在分布式算法领域,有位非常重要的短发叫Paxos,它的重要性有多高呢?Google的Chubby[1] ...

  5. C++笔记--异常

    引言 异常,让一个函数可以在发现自己无法处理的错误时抛出一个异常,希望它的调用者可以直接或者间接处理这个问题.而传统错误处理技术,检查到一个局部无法处理的问题时: 1.终止程序(例如atol,atoi ...

  6. Java50道经典习题-程序48 数字加密

    题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换.分析:例如原始 ...

  7. 用Pdg2.DLL解码PDG的境界

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2008.08.03 一.入门级原理:按照<用BCB实现超星格式转换为BMP格式>中说的方法调用Pdg2.DLL接 ...

  8. Python中dataframe\ array\ list相互转化

    import pandas as pd import numpy as np #创建列表 a1=[1,2,3] #arange函数:指定初始值.终值.步长来创建数组 a2=np.arange(0,1, ...

  9. loj #2023. 「AHOI / HNOI2017」抛硬币

    #2023. 「AHOI / HNOI2017」抛硬币   题目描述 小 A 和小 B 是一对好朋友,他们经常一起愉快的玩耍.最近小 B 沉迷于**师手游,天天刷本,根本无心搞学习.但是已经入坑了几个 ...

  10. Python 文件和异常

    一.从文件中读取数据 #!/usr/bin/env python with open('pi') as file_object: contents = file_object.read() print ...