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. Python 黑客 004 用Python构建一个SSH僵尸网络 01 简介

    用Python构建一个SSH僵尸网络 01 简介 一. 构建一个SSH僵尸网络的流程图: Created with Raphaël 2.1.0手动操作,实现通过SSH连接目标服务器(手动)用 Pexp ...

  2. Linux 使用静态库注意事项

    1. 静态库一定要放在生成文件后面 gcc main.c -o main libhello.a 2. 使用静态库时一定要连接所有用到的静态库 gcc main.c -o main liba.a lib ...

  3. scala中lazy

    Scala中通过lazy关键字来定义惰性变量,惰性变量只能是不可变变量.只有在调用惰性变量的时候,才会去实例化这个变量

  4. PDG科普篇

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2009.09.26 更新历史 2014.11.11补充了文字版PDG的部分内容增加CX PDF等打包格式的相关内容 2009 ...

  5. CentOS6.5上Zabbix3.0的RPM安装【一】-安装并配置Server

    一.Environment OS:CentOS6.5 64bit [桌面版安装] Server端:192.168.201.109 ServerName Clinet端:192.168.201.199 ...

  6. See Elevator Run Floors

    “在我短暂的电梯作业中我发现,架构的优化能力是有限的.越是工于优化算法…越是会被自己的架构所制约….想要更好的优化,唯有超越架构………" 零.基础 优化建立在架构之上,这句话莫得问题,也莫得 ...

  7. 洛谷 P3806 【模板】点分治1

    P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条边a,b,c描述 ...

  8. Q的进阶用法

    Q的实例化用法 #q1 里面的条件都是or的关系 q1=Q() q1.connector = 'OR' q1.children.append(('id',1)) q1.children.append( ...

  9. 2019.2.14 考试T3 交互题

    \(\color{#0066ff}{ 题目描述 }\) 由于机房被成功拯救了,花_Q很高兴,花_Q生成了一个 0 到 N - 1 的排列(排列的下标从 0 到 N - 1 ).保证排列中 0 在 N ...

  10. P2962 [USACO09NOV]灯Lights 对抗搜索

    \(\color{#0066ff}{题目描述}\) 贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗 ...