19. Remove Nth Node From End of List C++删除链表的倒数第N个节点
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
使用双指针法,可以仅遍历一次完成节点的定位
/**
* 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* l = new ListNode();
l = head;
ListNode* r = new ListNode();
r = l;
while(n--)
r = r->next;
//用于判断head是否为单节点链表
if(r == NULL)
return head->next;
while(r != NULL && r->next != NULL)
{
r = r->next;
l = l->next;
}
ListNode* temp = new ListNode();
temp = l->next;
l->next = l->next->next;
if(temp)
delete(temp);
return head;
}
};
19. Remove Nth Node From End of List C++删除链表的倒数第N个节点的更多相关文章
- 19. Remove Nth Node From End of List[M]删除链表的倒数第N个节点
题目 Given a linked list, remove the n-th node from the end of list and return its head. *Example: Giv ...
- LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)
题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...
- LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)
题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...
- 25.Remove Nth Node From End of List(删除链表的倒数第n个节点)
Level: Medium 题目描述: Given a linked list, remove the n-th node from the end of list and return its ...
- 力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现
题目描述: 中文: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二 ...
- [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 ...
- [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点
Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...
- 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 ...
随机推荐
- Chrome上的扩展工具
rest client - rest api Testing:类似fiddler的工具,可以测试url,查看返回值……
- vue动态改变样式
<i class="el-icon-arrow-left" :style="{'color': deadColorArr[index]}" @click= ...
- MyBatis数据库测试代码自动生成
<!-- generatorConfig.xml配置,其中:<plugin type="org.mybatis.generator.plugins.ToStringPlugin& ...
- 【Ruby】【基础】
# [Ruby 块]=begin1 块由大量代码构成2 块中代码包含在{}内3 从与其相同名称的函数调用4 可以使用yield语句调用块=enddef test p '在test方法内' yield ...
- _itemmod_enchant_groups
随机附魔组 附魔组 `groupId` 分组编号,同一groupId的附魔效果被随机抽取 `enchantId` 附魔Id 对应SpellItemEnchantment.dbc `chance` 被抽 ...
- hdu 3208 Integer’s Power 筛法
Integer’s Power Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D. Office Keys time limit per test2 seconds 二分
D. Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 面试题中关于String的常见操作
题目1: 将用户输入的一段话,每个单词的首字母大写, 每个单词之间的空格调整为只有一个,遇到数字,将数字与后一个单词用下划线 "_" 进行连接 题目2:将 i @@ am @@@ ...
- python模块(3)
1.xml:实现不同语言或程序之间进行数据交换的协议 <data> <country name="Liechtenstein"> <rank upda ...
- 《剑指offer》第五十题(字符串中第一个只出现一次的字符)
// 面试题50(一):字符串中第一个只出现一次的字符 // 题目:在字符串中找出第一个只出现一次的字符.如输入"abaccdeff",则输出 // 'b'. #include & ...