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 ...
随机推荐
- Ros学习——创建程序包
1.程序包 一个程序包要想称为catkin程序包必须符合以下要求: 该程序包必须包含catkin compliant package.xml文件 这个package.xml文件提供有关程序包的元信 ...
- docker创建容器打开两个端口
docker run -d -it --name c6_3 -v :/mnt -p 5000:8000 -p 3000 centos 注释: -v 后面为共享文件夹
- Freemarker 使用注意事项
上次借助 Freemaker 模板引擎来动态构造 Kylin 访问的 SQL,在使用过程中,遇到了一些坑. ${} 输出变量时需要注意: 示例 WHERE shop_id = ${val} 里的 v ...
- MySql 5.7中添加用户,新建数据库,用户授权,删除用户,修改密码
转自http://blog.csdn.net/w690333243/article/details/76576952 1.新建用户 创建test用户,密码是1234. MySQL -u root -p ...
- webfrom 母版页
ASP.NET中母版页作用 一是提高代码的复用(把相同的代码抽出来) 二是使整个网站保持一致的风格和样式. 母版页存在就一定要有内容页的存在,否则母版页的存在就没有了意义. .master 一.添加母 ...
- EZOJ #87
传送门 分析 由于我不知道壶里到底有多少水,那么显然我第一次 分别向两个杯子分别到 L/2 +1 和 L/2 才是最优的.(这样既维护了两个人的差值不超1,又正好倒了L的水).那么接下来如果壶里还有水 ...
- 在windows中安装OpenSSH,无密码登录,永远不断线
到这里下载最新安装包:https://github.com/PowerShell/Win32-OpenSSH/releases下载下来解压,然后用管理员打开服务器自带的PowerShell,运行下列命 ...
- patch请求--501错误
通过命令 tail -f /var/log/wildfly/wrapper.log -n 1000 查看控制台: 并没有报红. 未显示具体哪行代码有错误. debug运行patch请求,根本没有进入p ...
- LeetCode第110题:平衡二叉树
问题描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,nu ...
- (树)判断二叉树是否为BST
题目:判断一颗二叉树是否为BST. 思路:其实这个问题可以有多个解决方法. 方法一:递归解决.根据BST的特性.左边的小于根节点的值,右边的大于根节点的值.并且对于每一棵子树都是如此.所以我们可以直接 ...