删除指定节点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 ...
随机推荐
- Gym-102141E
https://vjudge.net/problem/Gym-102141E 用set乱搞 #include<iostream> #include<cstdio> #inclu ...
- vue 学习 二
动画 <transition name="fade"> <p v-if="show">hello</p> </tran ...
- Kafka在window上安装部署
1.准备工作 ①jdk 具体自行百度安装jdk,配置好 JAVA_HOME和path, 下载地址: http://www.oracle.com/technetwork/java/javase/ ...
- 模板:数位DP
第一次听说dp还有模板的... 当然你要是记忆化搜索的话,就可以有一些套路 这是一个伪代码: LL Dfs(LL now,限制,LL top){ if(!now) return 判断条件; if(!t ...
- Make the Most (Hackerrank Codeagon)
题目链接 Problem Statement Codenation is sending N of its employees to a High Profile Business Conferenc ...
- DES、RSA、MD5、SHA、随机生成加密与解密
一.数据加密/编码算法列表 常见用于保证安全的加密或编码算法如下: 1.常用密钥算法 密钥算法用来对敏感数据.摘要.签名等信息进行加密,常用的密钥算法包括: DES(Data Encr ...
- java普通for循环和for-each迭代循环的区别
PO实体类User: package aA; public class User { private String name; private int many; private int id; pu ...
- 2019-8-31-dotnet-获取用户设备安装了哪些-.NET-Framework-框架
title author date CreateTime categories dotnet 获取用户设备安装了哪些 .NET Framework 框架 lindexi 2019-08-31 16:5 ...
- TZ_16_Vue的v-model和v-on
1.v-model是双向绑定,视图(View)和模型(Model)之间会互相影响. 既然是双向绑定,一定是在视图中可以修改数据,这样就限定了视图的元素类型.目前v-model的可使用元素有: inpu ...
- js作用域的销毁、不立即销毁、不销毁
JavaScript中的函数执行会形成私有的作用域. (1)作用域的销毁 一般情况下,函数执行形成一个私有的作用域,当执行完成后就销毁了->节省内存空间 (2)作用域的不立即销毁 functio ...