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

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

  2. 【一天一道LeetCode】#24. Swap Nodes in Pairs

    一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...

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

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

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

  5. 【leetcode❤python】24. Swap Nodes in Pairs

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

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

  7. LeetCode:24. Swap Nodes in Pairs(Medium)

    1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...

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

  9. 24. Swap Nodes in Pairs

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

随机推荐

  1. vijos p1729 Knights

    描述 在一个N*N的正方形棋盘上,放置了一些骑士.我们将棋盘的行用1开始的N个自然数标记,将列用'A'开始的N个大写英文字母标记.举个例子来说,一个标准的8*8的国际象棋棋盘的行标记为1..8,列标记 ...

  2. OpenCV学习(33) 轮廓的特征矩Moment

    在OpenCV中,可以很方便的计算多边形区域的3阶特征矩,opencv中的矩主要包括以下几种:空间矩,中心矩和中心归一化矩. class Moments { public: ...... // 空间矩 ...

  3. 数学图形之将曲线(curve)转化成曲面管

    在我关于数学图形的博客中,一开始讲曲线的生成算法.然后在最近的章节中介绍了圆环,还介绍了螺旋管以及海螺的生成算法.一类是曲线,一类是环面,为什么不将曲线变成环的图形,毕竟曲线看上去太单薄了,这一节我将 ...

  4. linux下添加分区并挂载目录、卸载并删除分区

    添加分区并挂载目录 Linux的硬盘识别: 一般使用”fdisk -l”命令可以列出系统中当前连接的硬盘 设备和分区信息.新硬盘没有分区信息,则只显示硬盘大小信息.   1.关闭服务器加上新硬盘   ...

  5. Informatica 常用组件Lookup缓存之三 重建查找高速缓存

    如果您认为查找源在 PowerCenter 上次构建高速缓存时已更改,则可指示 PowerCenter 重建查找高速缓存. 重建高速缓存时,PowerCenter 会覆盖现有永久高速缓存文件而创建新的 ...

  6. Java使用jxl.jar包写Excel文件的最适合列宽问题基本实现

    以前用jxl.jar包,读写过Excel文件.也没有注意最适合列宽的问题,但是jxl.jar没有提供最适合列宽的功能,上次用到写了一下,可以基本实现最适合列宽. 注意,这个只是基本可以实现,基本针对中 ...

  7. Android中XML解析-SAX解析

    昨天由于时间比较匆忙只写了Android中的XML解析的Dom方式,这种方式比较方便,很容易理解,最大的不足就是内容多的时候,会消耗内存.SAX(Simple API for XML)是一个解析速度快 ...

  8. Android视频播放-SurfaceView和Mediaplayer

    好几天没写博客了,处理了一点个人私事加上平时加班,基本上时间不充裕,上篇文章讲了一下用Mediaplayer来播放音乐,这次就讲讲使用Mediaplayer来和SurfaceView配合播放一个视频流 ...

  9. 遇到问题描述:Android Please ensure that adb is correctly located at问题解决

    遇到问题描述: 运行android程序控制台输出 [2013-11-04 16:18:26 - ] The connection to adb is down, and a severe error ...

  10. 【Docker】容器、虚拟机与Docker概念全解析

    导读 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.本文立足于新手,从容器和虚拟机两个大 ...