61. Rotate 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)的更多相关文章

  1. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  2. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  3. 【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 ...

  4. [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 ...

  5. 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, ...

  6. [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, ...

  7. 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 ...

  8. 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 ...

  9. 【一天一道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 ...

随机推荐

  1. 【第三课】Centos 7.x系统安装和网络配置以及远程密钥登录

    目录 一.安装CentOS 7.3 二.配置网络 1.使用dhclient命令自动获取ip地址 2.使用ip addr或ifconfig命令查看网卡信息 3.使用route命令查看路由信息 4.通过修 ...

  2. TMS320VC5509的MCBSP配置成SPI模式通信

    1. 首先是把MCBSP的配置 其次是时钟停止模式的配置,关闭大同小异 SPI有4中模式,怎么根据上面的寄存器选择哪种模式?下面展示了其中两种,CLKXP=1的时候有另外两种,暂时不整出来了 2. 代 ...

  3. 移动端H5页面上传图片或多张图片

    传统PC网页上传文件,大家都已经熟悉,这里不做介绍. 本文简单介绍移动端常用上传图片功能.灵活使用轮询或长连接可实现PC与移动端数据同步,即PC端需要上传的图片是移动拍照下来或移动端硬盘储存的,不需要 ...

  4. linux之awk基础

    第一章 1.awk 简介 awk不仅仅时linux系统中的一个命令,而且是一种编程语言,可以用来处理数据和生成报告(excel).处理的数据可以是一个或多个文件,可以是来自标准输入,也可以通过管道获取 ...

  5. java Script复习总结

    一:基础知识 1.JavaScript语言的历史 l  早期名称:livescript l  开发公司:网景公司(netscape) 2.JavaScript语言的基本特点 l  基于对象 l  事件 ...

  6. openstack horizon 开发第二天

    依照上次的简单的仪表盘添加动作额外添加或修改的文件mydashboard/├── mypanel│   ├── forms.py│   ├── tables.py│   ├── templates│  ...

  7. ubuntu16.04下Hyperledger之搭建Fabric环境简单操作(五步启动e2e_cli)

    如果你已经安装好go等工具.git及checkout相关代及下载相关镜像,您只需下面5步就能up e2e_cli~/go/src/github.com/hyperledger/fabric$ sudo ...

  8. 四则运算ver.mk2

    package size; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JMenuBar; im ...

  9. Eat Style --proposed by Chongyang Bai

    NEED 1. 有人希望妈妈是这样的: 但实际上对妈妈做的菜反应确是这样的: 处在不同的时节,根据不同的个人偏好,到底该做些什么饭菜?工作繁忙,家里的厨师可能也没时间琢磨.最后做出的只是应付差事的饭菜 ...

  10. 操作系统学习(一)、80x86保护模式内存管理

    整理的不好,凑合着看吧 目录 1.内存及寻址 2.地址变换 3.分段机制 4.分页机制 5.保护 6.去到底部 一.内存及寻址 返回目录 二.地址变换 80X86 从 逻辑地址 到 物理地址 的转换: ...