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. SQL开发中容易忽视的一些小地方(二)

    原文:SQL开发中容易忽视的一些小地方(二) 目的:继上一篇:SQL开发中容易忽视的一些小地方(一) 总结SQL中的null用法后,本文我将说说表联接查询. 为了说明问题,我创建了两个表,分别是学生信 ...

  2. 学习笔记 broswerify + watchify + beefy

    broswerify “Browserify lets you require('modules') in the browser by bundling up all of your depende ...

  3. Spring AOP入门——概念和注意事项

    AOP什么? AOP在功能方面,它是之前和之后运行一些业务逻辑,一些操作(比方记录日志.或者是推断是否有权限等),这些操作的加入.全然不耦合于原来的业务逻辑.从而对原有业务逻辑全然是透明. 也就是说. ...

  4. getBoundingClientRect的用法

    getBoundingClientRect用于获取某个元素相对于视窗的位置集合.集合中有top, right, bottom, left等属性. 1.语法:这个方法没有参数. rectObject = ...

  5. Linux netstat订购具体解释

    简单介绍 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics).masquerade 连接.多播成员 (Multicast Memb ...

  6. 如何使用iOS 8 指纹识别,代码、示例

    像想象,iOS 8指纹是非常容易使用的.只是需要能够获得一个接口,弹出屏幕模式框,随着app store在相同的. 直接上的条形码.以下代码是从复制Apple官方文件. 加入LocalAuthenti ...

  7. linux tar.gz zip 减压 压缩命令

    http://apps.hi.baidu.com/share/detail/37384818 download ADT link http://dl.google.com/android/ADT-0. ...

  8. Oracle 树操作

    Oracle 树操作(select…start with…connect by…prior) oracle树查询的最重要的就是select…start with…connect by…prior语法了 ...

  9. 【Linux】Vim编辑器-批量注释与反注释

    [-] vim编辑器---批量注释与反注释 方法一 块选择模式 插入注释 取消注释 方法二 替换命令 批量注释 取消注释 实例演示   vim编辑器---批量注释与反注释 在使用vim编写代码的时候, ...

  10. javascript中用来定义引用类型的一种"默认"模式

    // 终极版:组合使用构造函数模式和原型模式:***************************** // 评价:集构造函数模式和原型模式之大成: 用来定义引用类型的一种默认模式 function ...