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 ...
随机推荐
- VS与Opencv的亲密接触之安装配置过程
最近想把FPGA采集的图像,上传到上位机显示,看到Opencv能帮大忙,所以就折腾折腾! 我用的是VS2012和opencv-2.4.10-2.4.10(目前的最新版本),那个版本无所谓,本文都将适用 ...
- cocos2d-x学习记录3——CCTouch触摸响应
游戏不同于影音,强交互性是其一大特色,在游戏中主要体现为接受用户的输入并响应.智能手机触摸是其重要的输入方式. 在cocos2d-x中,触摸分为单点触摸和多点触摸. 单点触摸:主要继承CCTarget ...
- libgdx学习记录18——Box2d物理引擎
libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...
- HashMap 源码解析(一)之使用、构造以及计算容量
目录 简介 集合和映射 HashMap 特点 使用 构造 相关属性 构造方法 tableSizeFor 函数 一般的算法(效率低, 不值得借鉴) tableSizeFor 函数算法 效率比较 tabl ...
- 设计模式 笔记 单例模式 Singleton
//---------------------------15/04/09---------------------------- //Singleton 单例模式-----对象创建型模式 /* 1: ...
- eclipse + maven + com.sun.jersey 创建 restful api
maven 创建 jersey 项目 如果没找到 jersey archetype, 下载 maven 的 archetype xml, 然后导入 archetypes 运行 右击 main.java ...
- Unity EasyTouch官方案例学习
一.代码检测手势事件 1. EasyTouch4.x 写法 首先要手动在 Hierarchy 窗口添加 EasyTouch 物体,以触摸(Touch)手势为例,代码如下: using UnityEng ...
- C语言 -- 字符串详解
字符串是一种非常重要的数据类型,但是C语言不存在显式的字符串类型,C语言中的字符串都以字符串常量的形式出现或存储在字符数组中.同时,C 语言提供了一系列库函数来对操作字符串,这些库函数都包含在头文件 ...
- 红黑树插入与删除完整代码(dart语言实现)
之前分析了红黑树的删除,这里附上红黑树的完整版代码,包括查找.插入.删除等.删除后修复实现了两种算法,均比之前的更为简洁.一种是我自己的实现,代码非常简洁,行数更少:一种是Linux.Java等源码版 ...
- 作业 20181204-4 互评Final版本
此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2478] 组名:可以低头,但没必要 组长:付佳 组员:张俊余 李文涛 孙 ...