Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

For example, Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

注意: 前两个互换的时候,head 要改变位置。还要有一个 pre 指针。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
ListNode *pre = NULL, *p = head;
while(p && p->next) {
ListNode *q = p->next;
p->next = q->next;
q->next = p;
if(pre == NULL) head = q;
else pre->next = q;
pre = p;
p = p->next;
}
return head;
}
};

Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.

For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.

注意: 前两个互换的时候,head 要改变位置。还要有一个 pre 指针。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
int getLength(ListNode *head) {
int len = 0;
while(head) {
++len;
head = head->next;
}
return len;
}
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
if(head == NULL) return NULL;
k = k % getLength(head);
if(k == 0) return head;
ListNode *first, *second, *preFirst;
first = second = head;
for(int i = 1; i < k && second->next; ++i) // k-1 step
second = second->next;
//if(second->next == NULL) return head;
while(second->next) {
preFirst = first;
first = first->next;
second = second->next;
}
second->next = head;
preFirst->next = NULL;
return first;
}
};

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 *pre = NULL, *p1, *p2;
p1 = p2 = head;
for(int i = 1; i < n; ++i) p2 = p2->next;
while(p2->next) {
pre = p1;
p1 = p1->next;
p2 = p2->next;
}
if(pre) pre->next = pre->next->next;
return pre ? head : head->next; }
};

63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List的更多相关文章

  1. Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  2. Leetcode-24 Swap Nodes in Pairs

    #24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  3. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  4. [LintCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head.   Example Given 1->2-> ...

  5. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  6. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  7. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

    Swap Nodes in Pairs  Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  8. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

  9. leetcode-algorithms-24 Swap Nodes in Pairs

    leetcode-algorithms-24 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and re ...

随机推荐

  1. Serv-U FTP之PASV和PORT模式

    Serv-U 设置好后,访问,却提示如下错误:ftp服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹.在解决此问题前,我亲自遇到该问题,看看我查的资料 FTP的连接一般是有两个连接的,一个是客 ...

  2. url编码 中文在url参数中传递,在请求头,响应头中传递,是如何编码的呢?

    一定要编码成url的吗?还是url自动把接受的汉字编码,请求头响应头到达之后再自动编码成汉字?这样似乎比较合理哦 先把iso8859-1 转换成 utf-8,在mvc中处理,然后响应的时候在转成iso ...

  3. JavaScript字符串常用操作函数之学习笔记

    字符串简介 使用英文单引号或双引号括起来,如:’Hello’,”World”,但是不能首尾的单引号和双引号必须一致,交错使用,如果要打印单引号或者双引号,可以使用转义字符\’(单引号),\”(双引号) ...

  4. 【转】使用Sublime + PlantUML高效地画图

    project: blog status: publish target: how-to-use-sublime-and-plant-uml-draw-diagram.md date: 2015-12 ...

  5. javaweb---html标签

    img标签

  6. git 添加ssh的方法 push免登陆

    在github.com上 建立了一个小项目,可是在每次push  的时候,都要输入用户名和密码,很是麻烦 原因是使用了https方式 push 在termail里边 输入  git remote -v ...

  7. Flask生成SECRET_KEY(密钥)的一种简单方法

    SECRET_KEY是Flask中比较重要的一个配置值.本文介绍一种比较简单的生成SECRET_KEY的方法. Session, Cookies以及一些第三方扩展都会用到SECRET_KEY值,这是一 ...

  8. Spring使用——环境部署和配置问题总结

    众所周知,spring是Java中一个非常非常重要的框架,主要提供了依赖注入DI,和切面编程AOP.我多年前做过一段时间的Java,不过那时候项目中没有用Spring,所以一直也没有特别注意,最近看了 ...

  9. PAT (Basic Level) Practise:1026. 程序运行时间

    [题目链接] 要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock t ...

  10. 华为HG255D路由器使用OH3C进行中大校园网认证

    之前用的上海贝尔RG100A-AA路由器,被我无情地摧残了,电源按钮挂了,只能换个路由器.由于在校内,使用OP还是比较方便的,网上淘了这款华为HG255D,店主已刷好OP,无线速率300M,想想也是值 ...