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. href="javascript:void(0);"与#差异

    将<a>标签设置为空链接有两种方式.第一个是href="#",第二个是href="javascript:void(0);". 两种方式都设置了标签为 ...

  2. 你如何破解后安装PS cs6

    至于破解程序猿支持,资源共享是只有更好的,我相信有很多孩子还在用cs5看版本号.假设你想尝试新的版本号PS.但很长一段时间不能找到字的串行数.现在,你如何支付你的序列号和使用永久裂纹PS cs6. 好 ...

  3. FPGA 设计流程,延迟,时间

    FPGA 设计流程,延迟,时间 流程:每个时钟周期可以传输的数据比特. 延迟:从输入到时钟周期的输出数据需要经验. 时间:两个元件之间的最大延迟,最高时钟速度. 1 採用流水线能够提高 流量: 比如计 ...

  4. 采购申请 POCIRM-001:ORA-01403: 无论数据未找到

    今天就让同事帮忙看问题.当请求生成采购订单,在销售模块错误提交销售订单 查看请求日志 +-------------------------------------------------------- ...

  5. 实现 mouse-drag 的图标拖动

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. 第20章 状态模式(State Pattern)

    原文 第20章 状态模式(State Pattern) 状态模式  概述:   当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式主要解决的是当控制一个对象状态的条件表 ...

  7. int有符号和无符号类型内存 -- C

    /* int 有符号 0xffffffff == -1 0xfffffffe == -2 最小 0x80000000 == -21 4748 3648 最大 0x7fffffff == 21 4748 ...

  8. 前端学习笔记(zepto或jquery)——对li标签的相关操作(一)

    对li标签的相关操作——点击li标签进行样式切换的两种方式 Demo演示: 1 2 3 4 // 详解: 第一种方式(以ul为基础): $("ul").bind("cli ...

  9. 2014年辛星jquery解读第二节

    *************jquery的语法****************** 1.jquery是通过选取HTML元素,而且对选取的元素运行某些操作,从而完毕某些特效的. 2.因此,我们在使用jQu ...

  10. 开始 space viking 之旅

     设备 cocos2d-v2 眼下cocos2d-v3也不太稳定,它在很大程度上仍然是变化的功能. 对于稳定.我们仍然使用 v2 wget -c http://cocos2d-iphone.goo ...