61. Rotate List(M);19. Remove Nth Node From End of List(M)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:
Given ->->->->->NULL and k = ,
return ->->->->->NULL.
- Total Accepted: 102574
- Total Submissions: 423333
- Difficulty: Medium
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
if (head == nullptr || k == ) return head;
int len = ;
ListNode* p = head;
while (p->next) { //
len++;
p = p->next;
}
k = len - k % len;
p->next = head; //
for(int step = ; step < k; step++) {
p = p->next; //
}
head = p->next; //
p->next = nullptr; //
return head;
}
};
16ms 19.35%
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {// by guxuanqing@gmail.com
public:
ListNode* rotateRight(ListNode* head, int k)
{
if(NULL == head || NULL == head->next) return head;//null or one node
ListNode *p = NULL, *q = head;
int len = ;
while (q->next)//cal the length of the list
{
++len;
q = q->next;
}
q->next = head;
k %= len;
int tmp = len - k;
q = head;
while (--tmp)
{
q = q->next;
}
head = q->next;
q->next = NULL;
return head;
}
};
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, 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.
- Total Accepted: 169535
- Total Submissions: 517203
- Difficulty: Medium
/**
* 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 dummy(-);
dummy.next = head;
ListNode *p = &dummy, *q = &dummy;
for (int i = ; i < n; i++)//q先走n步
q = q->next;
while(q->next) {
p = p->next;
q = q->next;
}
ListNode *tmp = p->next;
p->next = p->next->next;
delete tmp;
return dummy.next;
}
};
6ms 64.75%
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {// by guxuanqing@gmail.com
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(NULL == head || NULL == head->next) return NULL;//null or one node
ListNode dummy(-);
dummy.next = head;
ListNode *p = &dummy, *q = &dummy;
int tmp = n;
while (tmp--)
{
q = q->next;
}
while (q->next)
{
q = q->next;
p = p->next;
}
ListNode *rnode = p->next;
p->next = rnode->next;
delete rnode;
return dummy.next;
}
};
9ms 23.47%
61. Rotate List(M);19. Remove Nth Node From End of List(M)的更多相关文章
- 《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 ...
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
随机推荐
- 查看Oracle数据库中的,已经连接好的..当前用户状况
参考: http://stackoverflow.com/questions/1043096/how-to-list-active-open-connections-in-oracle 以sys身份连 ...
- stm32f051 DMA需要注意的一点
STM32f051的DMA注意事项 问题说明:在使用ADC的DMA通道时,遇到了序列转换的乱序问题,我使用的是DMA循环模式,但是采集的数据却总是错的:第二个内存地址存放的是ADC序列转换中的第一个通 ...
- [BZOJ1135][POI2009]Lyz[霍尔定理+线段树]
题意 题目链接 分析 这个二分图匹配模型直接建图的复杂度太高,考虑霍尔定理. 对于某些人组成的区间,我们只需要考虑他们的并是一段连续的区间的集合.更进一步地,我们考虑的人一定是连续的. 假设我们考虑的 ...
- Elasticsearch date 类型详解
引言 一直对 elasticsearch 中的 date 类型认识比较模糊,而且在使用中又比较常见,这次决定多花些时间,彻底弄懂它,希望能对用到的同学提供帮助. 注意:本文测试使用是 elastics ...
- .Net-C#异步程序知识点梳理
:first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...
- Microsoft Dynamics CRM 常用JS语法(已转成vs2017语法提示)
背景 最近接触到Microsoft Dynamics CRM的开发.前端js是必不可少的部分,奈何没有一个语法提示,点不出来后续的语句. 在vscode上面搜索插件的时候发现,有一个大神写的插件htt ...
- Unity3D — — UGUI之简易背包
Uinity版本:2017.3 最近在学Siki老师的<黑暗之光RPG>教程,由于教程内用的是NGUI实现,而笔者本人用的是UGUI,所以在这里稍微写一下自己的实现思路(大致上和NGUI一 ...
- leetcode刷题笔记258 各位相加
题目描述: 给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字. 例如: 设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返 ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...