【LeetCode】24. Swap Nodes in Pairs (3 solutions)
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.
解法一:递归
/**
* 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) {
if(head == NULL || head->next == NULL)
return head;
ListNode* newhead = head->next;
head->next = swapPairs(newhead->next);
newhead->next = head;
return newhead;
}
};

解法二:Reverse Nodes in k-Group令k=2
/**
* 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) {
return reverseKGroup(head,);
}
ListNode *reverseKGroup(ListNode *head, int k) {
ListNode* newhead = new ListNode(-);
ListNode* tail = newhead;
ListNode* begin = head;
ListNode* end = begin;
while(true)
{
int count = k;
while(count && end != NULL)
{
end = end->next;
count --;
}
if(count == )
{//reverse from [begin, end)
stack<ListNode*> s;
while(begin != end)
{
s.push(begin);
begin = begin->next;
}
while(!s.empty())
{
ListNode* top = s.top();
s.pop();
tail->next = top;
tail = tail->next;
}
}
else
{//leave out
tail->next = begin;
break;
}
}
return newhead->next;
}
};

解法三:
每两个节点成对交换次序后,返回给前一个结点进行连接。
/**
* 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* newhead = new ListNode(-);
newhead->next = head;
ListNode* tail = newhead;
while(tail->next != NULL && tail->next->next != NULL)
{
ListNode* A = tail->next;
ListNode* B = A->next; //swap
A->next = B->next;
B->next = A;
tail->next = B; tail = tail->next->next;
}
return newhead->next;
}
};

【LeetCode】24. Swap Nodes in Pairs (3 solutions)的更多相关文章
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 【LeetCode】024. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【leetcode❤python】24. Swap Nodes in Pairs
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- LeetCode OJ 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- LeetCode:24. Swap Nodes in Pairs(Medium)
1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...
- 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
随机推荐
- @Java虚拟机之对象访问
建立对象是为了使用对象,我们的java程序需要通过栈上的reference数据来操作堆上的具体对象. 对象访问会涉及到Java栈.Java堆.方法区这三个内存区域. 如下面这句代码: Object o ...
- iOS开发-CoreMotion框架(加速计和陀螺仪)
CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速度计和陀螺仪,在iOS4之前加速度计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来 ...
- VB.NET与C# 语法区别展示
在学习VB.NET后发现,VB.NET与C#的语法主要的不同在两个部分,这两部分搞通了,那就游刃有余,迎刃而解了.现将其对比总结如下: 一.实体部分 (与VB相比,在C#和VB.NET中,实体的使用很 ...
- 转: 用 Eclipse 平台进行 C/C++ 开发
http://www.ibm.com/developerworks/cn/linux/opensource/os-ecc/index.html
- Unity异步加载场景loading条
using UnityEngine; using System.Collections; public class LoadingScene : MonoBehaviour { public UISl ...
- Diablo 3 Web API
这是暴雪提供的WebAPI,能够通过网页的方式訪问玩家的用户信息.结构相当清晰,非常有借鉴价值. 应用JSON(JavaScript Object Notation)做数据交换,很好理解. 以我的暗黑 ...
- loadrunner运行乱码解决方法
最近进行项目压力测试,选择用loadrunner来进行测试,当在回放时间,脚本中的中文由乱码.快照,经研究,经过一下3步可以解决,分享一下 第一步:当在进行新建脚本时间,选择选项中-〉高级-〉选择“支 ...
- java各种框架的比较,分析
Spring 框架 优点 1.提供了一种管理对象的方法,可以把中间层的对象有效地组织起来 2.采用了分层结构,可以增量引入到项目中. 3.代码测试较容易 4.非侵入性,应用程序对Spring API的 ...
- eclipes设置默认注释作者
打开Perfences首选项页,搜索Template,找到Java>> Code Style >> Code Template功能,在打开的树中,找到Comments.Type ...
- [C#]记录程序耗时的方法【转发】
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // H ...