63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
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的更多相关文章
- 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 ...
- 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 ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 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 ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- 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 ...
随机推荐
- nopcommerce 初学2
好久没有接触nopcommerce了. 2016-9-5 现在最新的是3.8. 这段时间也稍微接触了下aspnet 的mvc. 所以就想到nop是一个开源的 很全得项目, 然后就拆了一些可以学习跟借 ...
- Android M新特性之Behavior Changes
1.Runtime Permissions On your apps that target the M Preview release or higher, make sure to check f ...
- hive的基本操作
1.创建表 First, create a table with tab-delimited text file format: (1)CREATE TABLE u_data ( userid INT ...
- OpenResty 安装及使用(第一篇安装)
OpenResty搭建 1.openResty介绍 OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器.它打包了标准的 Nginx 核心,很多的常用的第三方模 ...
- Windows下利用py2exe生成静默运行的命令行程序
py2exe是python的第三方库,可以利用它将你的python脚本编译成可执行文件(exe),而在实际的开发过程中生成的dos窗口很影响用户体验,建议按以下方式让exe静默运行. 首先将你的pyt ...
- 从零开始学习Node.js例子三 图片上传和显示
index.js var server = require("./server"); var router = require("./router"); var ...
- 导出带图形的数据excel表
public static string StatisticsSR(string parmStr) { try { StatisticsSRInfo parm = JsonConvert.Deseri ...
- 简单介绍Javascript匿名函数和面向对象编程
忙里偷闲,简单介绍一下Javascript中匿名函数和闭包函数以及面向对象编程.首先简单介绍一下Javascript中的密名函数. 在Javascript中函数有以下3中定义方式: 1.最常用的定义方 ...
- android setVisibility失效不起作用的问题
原因:不同的布局中有名字相同的控件,所以adapter中填充item的时候,由于控件名字相同没有正确识别你要显示的控件. 解决方法:给其中一个控件改一下名字就行了,超简单.
- Centos7.2 Systemd 方式编译 Mysql5.7.11
导读 MySQL 5.7 版本的发布,也就是说从现在开始5.7已经可以在生产环境中使用,有任何问题官方都将立刻修复. MySQL 5.7主要特性: 原生支持Systemd 更好的性能:对于多核CPU. ...