因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余。

代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接;res是交换后的l指针,用于本组交换后尾指针在下一组交换后链接上交换后的头指针。

/**
* 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* l = head;
ListNode* r = head;
if(head == NULL || head->next == NULL)return head;
ListNode* res = NULL;
r = r->next;
head = r;
ListNode* temp;
while(r != NULL)
{
temp = r->next;
l->next = r->next;
r->next = l;
if(r != NULL && res != NULL)res->next = r;
res = l;
if(temp == NULL) break;
l = temp;
r = l->next;
}
return head;
}
};

讨论中看到比较简洁的解法,但可能消耗资源较多:

/**
* 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 **pp = &head, *a, *b;
while ((a = *pp) && (b = a->next)) {
a->next = b->next;
b->next = a;
*pp = b;
pp = &(a->next);
}
return head;
}
};

leetcode个人题解——#24 Swap Nodes in Pairs的更多相关文章

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

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

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

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

  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. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

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

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

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

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

  8. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  9. leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现

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

随机推荐

  1. MyBatis-Plus工具快速入门使用

    MyBatis-plus有什么特色 1.代码生成 2.条件构造器 对我而言,主要的目的是使用它强大的条件构建器. 快速使用步骤: 1.添加pom文件依赖 <dependency> < ...

  2. python 变量定义

    变量定义 什么是变量? 在程序运行过程中,其值可以改变的量. 标识符(命令规范) 只能由数字.字母.下划线组成 不能以数字开头 不能是系统关键字 import keyword ​ # 打印关键字列表 ...

  3. Centos6_32位系统512M内存_如何安装gogs_Mysql_配置开机自启动

    因为有很多人的Linux版本比较低,内存配置也较低,X86 ,32位系统的:所以这里推荐采用二进制安装gogs,并且使用Mysql:这个是傻瓜式的安装方案,适合绝大多数人(提及了centos7的安装思 ...

  4. 解决 LLVM 错误提示 may only occur zero or one times!

    使用 LLVM 混淆器添加参数进行编译提示如下错误:clang (LLVM option parsing): for the -bcf option: may only occur zero or o ...

  5. phpstudy lamp

    phpStudy for Linux (lnmp+lamp一键安装包 现在不考虑安装这个  (完整版:http://lamp.phpstudy.net/phpstudy-all.bin) 安装: wg ...

  6. linux 操作系统下简单的命令行操作

    一: 配置linux 操作系统虚拟主机 首先安装一个虚拟机(百度上面有很多哦) , 我主要使用的是VMware workstation 然后 下载一个centos镜像6..5到7都可以; 然后用VMw ...

  7. 对象转换成JSON字符串

    定义一个Student类: 1 class Student { 2 public $name; 3 public $age; 4 function __construct($name, $age) { ...

  8. Anaconda下的python如何写入环境变量中

    Anaconda是一个非常好的python管理软件,实际使用起来要比直接用python自带的管理工具更好. 若需要将Anaconda下的python.exe添入环境变量中,需要如下设置 如上图所示,需 ...

  9. python 爬虫(爬取网页的img并下载)

    from urllib.request import urlopen # 引用第三方库 import requests #引用requests/用于访问网站(没安装需要安装) from pyquery ...

  10. linux线程篇 (三) 线程的同步

    1 互斥量 pthreat_mutex_t mymutex; //1. 创建 初始化 int pthread_mutex_init(pthread_mutex_t *mutex, const pthr ...