Problem:

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.

本题要我们通过节点的操作来两两置换,而不是通过修改val的值。这题主要就是考察对链表指来指去,最后指向哪里是否清楚。应该要这样考虑,对于 1 2 3 4,先设置一个头指向这个表,假设为0,则0指向1,现在我们想要的结果是0指向2指向1指向3指向4.一步一步来,先将2的next赋值给1的next,然后把1赋值给2的next,这样就有了 2指向1指向3.(如果先把1直接赋值给2的next,那么这是3就被1覆盖了,找不到3了,所以不能这样做)。我们已经有了2指向1指向3指向4,因为0指向的还是1,没改,所以这个时候要将0指向2了,那么就有了0指向2指向1指向3指向4,还没有结束,因为3和4还没有置换。同理,这个时候要先把4的next给3,再把3给4的next。这时候是不是就完了呢,不是的,因为1指向的还是3,所以还需要将4给1的next(这个通过代码中的bef(就是before的意思)来实现,每次把bef赋值为已经置换好的第二个,再把下一个置换好的头赋值给bef的next,就把整个串好了。因为我们的头是0,所以最后返回0的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)
{
if(!head || !(head -> next))
{
return head;
}
ListNode *tmp = new ListNode();
ListNode *ans = tmp;
ListNode *bef = ans; // 用来保证将ans指向下一个二元组
tmp -> next = head;
tmp = tmp -> next;
while(tmp && (tmp -> next))
{
ListNode *m = tmp;
ListNode *nn = tmp -> next;
tmp = tmp -> next;
m -> next = nn -> next;// 例如 1 2 3,先把1指向3,再把2指向1,这样的话就是2 -> 1 -> 3
tmp -> next =m;
bef -> next = tmp; // bef的下一个要指向下一个二元组的第一个
tmp = m -> next;
bef = m;// 更新bef为转换好的二元组的后一个,为了使得和下一个二元组连接
}
return ans -> next; // ans 的第一个为零,next开始才是所要的
}
};

我在return上面加一个delete ans,居然还是accept。这题规模小所以new的不delete可以。如果我想要delete呢。应该如何?

leetcode第23题--Swap Nodes in Pairs的更多相关文章

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

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

  3. LeetCode(24) Swap Nodes in Pairs

    题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

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

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

  5. leetcode个人题解——#24 Swap Nodes in Pairs

    因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...

  6. 乘风破浪:LeetCode真题_024_Swap Nodes in Pairs

    乘风破浪:LeetCode真题_024_Swap Nodes in Pairs 一.前言 这次还是链表的操作,不过我们需要交换链表奇数和偶数位置上的节点,因此要怎么做呢? 二.Swap Nodes i ...

  7. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

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

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

  9. LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation

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

随机推荐

  1. 算法----选择排序(select sort)

    排序不是一个时间的数组进行排序,找到最小的元素,其与阵列的第一个元素交换,因此,排序的数组. 算法: void sort::select_sort(int* a,const int n) { for( ...

  2. MapReduce 规划 系列十 采用HashPartitioner调整Reducer计算负荷

    example4它演示了如何指定Reducer号码,本节演示如何使用HashPartitioner将Mapper根据该输出key分组后Reducer为了应对. 合理的分组策略会尽一切Reducer不能 ...

  3. 具体解释首页被K后SEOer必做的三大排除方法!

    近段时间.有非常多朋友向新辰抱怨说出大问题了,为神马site不到首页了,并且收录变成了0?唉,新辰不得不非常同情的告诉你:你的首页真的被K了!好了.作为一个职业SEOer.面对被K宛如已经看破红尘般没 ...

  4. Oracle存储过程详解(引用)+补充(转) dbms_output包 good

    执行存储过程时,execute和call的区别 EXEC is a sqlplus command that put its argument as an anonymous pl/sql block ...

  5. CIC and Fir 滤波器的级联

    在FDATool中 CIC 和 Fir 级联滤波器的设计 1 设计CIC滤波器的幅频特性曲线如下 2.设计FIR 滤波器的幅频特性曲线如下 3.总的特性曲线如下 4.把通带部分放大后的图,比较平坦

  6. 离robots.txt启动网络爬虫之旅

    要成为一个网络爬虫或搜索引擎(在这里,共同蜘蛛)它不会陌生,在搜索引擎爬虫的第一个文件或者访问该网站上浏览robots.txt该.robots.txt文件讲述了蜘蛛server哪些文件要观看正在. 当 ...

  7. 我的Linux学习历程:那些我看过的Linux书籍们

    [+]查看原图http://www.ituring.com.cn/article/119401 来北京工作已经一个多月,大都市的生活比起读大学要忙碌得多,尤其是出行,基本以小时为基本的计时单位.有时茫 ...

  8. java json字符串转List、Map等对象

    List<Map<String, Object>> map = g.fromJson(jsonStr, new TypeToken<List<Map<Stri ...

  9. Android手游《》斗地主完整的源代码(支持单机和网络对战)

    Android手游<斗地主>完整的源代码(支持单机和网络对战)下载.一个很不错的源代码. 斗地主掌游是一个独特的国内社会斗地主棋牌游戏,之后玩家可以下载网上斗地主和全世界.掌游斗地主特点: ...

  10. Ubuntu通过使用PyCharm 进行调试 Odoo 8.0 可能出现的问题

    实现步骤,请移步http://shine-it.net/index.php?topic=16603.0 要么 http://www.mindissoftware.com/2014/09/11/Run- ...