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. 百度echarts扇形图每个区块增加点击事件

    效果图:操作人员要求 :我想看这个扇形图对应的 页面信息,给我加个链接跳转:原先的chart.js发现没有api,后来改用百度的echart.js <!DOCTYPE html> < ...

  2. Oracle存储过程的简单示例

    ---创建一个表 create table TESTTABLE (   id1  VARCHAR2(12),   name VARCHAR2(32) ) select t.id1,t.name fro ...

  3. HDU 2082-找单词(母函数)

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. DevExpress.XtraReports.UI.XtraReport 动态报表

    原文:DevExpress.XtraReports.UI.XtraReport 动态报表 using System;using System.Collections.Generic;using Sys ...

  5. git branch(转)

    git branch    git branch 不带参数:列出本地已经存在的分支,并且在当前分支的前面加“*”号标记,例如:   #git branch* master   newbranch gi ...

  6. OR1200数据Cache介绍

    以下摘录<步骤吓得核心--软-core处理器的室内设计与分析>一本书 上一章剖析了ICache模块. 本章将剖析DCache模块.首先指出DCache模块相比ICache的特别之处.由于这 ...

  7. hdu 1700 Points on Cycle 水几何

    已知圆心(0,0)圆周上的一点,求圆周上另外两点使得三点构成等边三角形. 懒得推公式,直接用模板2圆(r1=dist,r2=sqrt(3)*dist)相交水过 #include<cstdio&g ...

  8. 大数据系列修炼-Scala课程08

    接下来会讲解关于各种模式匹配,从中就会知道模式匹配的重要性 关于Type.Array.List.Tuple模式解析 1.Type模式匹配代码解析 //关于Type类型的模式匹配 //匹配 Int类型. ...

  9. AspNetPager使用指南

    一.AspNetPager支持两种方式分页: 一种是PostBack方式分页, 一种是通过Url来实现分页以及Url重写功能 二.AspNetPager支持各种数据绑定控件GridView.DataG ...

  10. kobject_create_and_add

    本文来源于源linux 3.14.3版本号/lib/kobject.c文件 /**  * kobject_create_and_add - 动态地创建kobject结构和寄存器sysfs  *  * ...