删除指定节点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,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode *temp = head;
int len=,m;
while(temp){
++len;
temp=temp->next;
}
if(n==len)
return head->next;
m=len-n;
ListNode *temp1=head;
while(m>){
temp1=temp1->next;
--m;
}
temp1->next=temp1->next->next;
return head; }
};
删除指定节点Remove Nth Node From End of List的更多相关文章
- [Swift]LeetCode19. 删除链表的倒数第N个节点 | Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- LeetCode 19:删除链表的倒数第N个节点 Remove Nth Node From End of List
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. Given a linked list, remove the n-th node from the end of list and ...
- 【LeetCode每天一题】Remove Nth Node From End of List(移除链表倒数第N个节点)
Given a linked list, remove the n-th node from the end of list and return its head. Example: ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【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解题报告—— 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 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- Merge Two Sorted Lists & Remove Nth Node From End of List
1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...
随机推荐
- uni-app中不使用scroll-view组件,监听页面滑直底部事件
最终达到的目标效果 将要用到 监听页面滚动事件:onPageScroll 获取节点信息uni.createSelectorQuery() 标签布局 <template> <view ...
- WPF DataGrid 数据绑定之"List配合Dictionary"
WPF 的DataGrid是WPF中最为强大的控件之一,可以通过各种方式绑定 例如通过最为形似的dataTable来绑定 本文则用List<Dictionary<K,V>>来绑 ...
- PAT甲级——A1041 Be Unique
Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...
- ORACLE忘记sys密码
1.win+R打开dos窗口cmd 2.输入 sqlplus/nolog出现 3.输入 conn / as sysdba 出现 4.修改密码 alter user 用户 identified by 新 ...
- 进一步封装poco下的mysql操作
为方便程序对mysql操作,我对poco的mysql进行了再次封装,主要是针对自己应用需要的部分. 开发工具:netbean 系统环境:centos7 poco版本: poco-1.9.0-all 主 ...
- nginx 下开启pathinfo模式
前几天自己新弄了个服务器,nginx的环境, 看到thinkcmf的框架,下载下来准备研究下,安装完成后,发现 url 是 普通模式,然后我就按照那个手册去后台开启了pathinfo模式,这一改完蛋了 ...
- TZ_05_Spring_Proxy基于接口的动态代理和基于类的动态代理
代理:为了增强方法在不添加代码的情况下 1.Proxy基于接口的动态代理 /** * 模拟一个消费者 * @author Administrator * */ public class Client ...
- vw单位相关
1.相对于视口的宽度.视口被均分为100单位的vw h1 { font-size: 8vw; } 如果视口的宽度是200mm,那么上述代码中h1元素的字号将为16mm,即(8x200)/100 2.相 ...
- CodeChef--Cards, bags and coins
题目链接 Yet another game from chef. Chef gives you N cards and M bags. Each of the N cards has an integ ...
- 读书笔记--Struts 2 in Action 目录
1.Struts 2:现代Web框架 1.1 web应用程序:快速学习 21.1.1 构建web应用程序 21.1.2 基础技术简介 31.1.3 深入研究 61.2 web应用程序框架 71.2.1 ...