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

代码中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. 轻量ORM-SqlRepoEx (六) JOIN

    示例使用的是最新 SqlRepoEx 2.0.2 可在:https://github.com/AzThinker/SqlRepoEx2.0Demo 或:https://gitee.com/azthin ...

  2. CSS实现图片等比例缩小不变形

    <img src="../images/bg1.jpg" alt="" /> img { /*等宽缩小不变形*/ /*width: 100%;*/ ...

  3. 获取DOM

    <template> <div> <header-vue :msg="msg" ref="header">heheh< ...

  4. Java代码注释

    单行注释: 选中代码,按下ctrl+/ 一条代码单行注释:选中一条代码按下ctrl+/,则为一条代码单行注释: 多条代码单行注释:选中多条代码按下ctrl+/,则为多条代码单行注释: 取消注释:对已经 ...

  5. wdcp v3 pureftpd 无法登录问题解决

    wdcp v3 新建站点和ftp账号 单位无法登录ftp 在端口中也确实可以看到有进行在登录状态 错误原因: 防火墙端口没有开启该端口范围  20000-30000 这时候发现 改端口为20078  ...

  6. ecshop 后台添加新菜单 以及 权限控制

    首先 在languages\zh_cn\admin\common.php 中添加 一级菜单 二级菜单 其次 在admin\includes\inc_menu.php 中添加 然后 在admin\inc ...

  7. ElasticSearch 集群安装,简单使用

    http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html https://gith ...

  8. QP总体结构

    QP是一个基于事件驱动的嵌入式系统软件框架,其总体结构如下图. AO活动对象由事件队列和层次状态机两部分组成,每个AO占有一个优先级: QF量子框架由五个数据结构及操作组成,其数据结构采用了uCOS- ...

  9. web頁面優化以及SEO

    轉載:https://blog.csdn.net/xustart7720/article/details/79960591 浏览器访问优化浏览器请求处理流程如下图: Etag:實體標籤.ETag是HT ...

  10. Python学习笔记三:数据类型

    数据类型 整数int 32位机器,-2**31~2**31-1,即-2147483648~2147483647(4亿多) 64位机器,-2**63~2**63-1,非常大了. 长整型long 没有位数 ...