【转载请注明】http://www.cnblogs.com/igoslly/p/8707274.html

来看一下题目:

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.

题目意思:

将每两个相邻的结点互换,不可申请额外空间,对结点本身操作

思路:

1、这题考基础的链表结点操作,并没有多大难度,基本操作如下:

2、循环体 p 指向调换的前一个结点,为了避免 head ==NULL 和 链表单结点的情况,设置 ListNode * res ,res->next = head 指针


class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode * res=new ListNode();
res->next=head;
ListNode *p=res;
while(){
if(p->next!=NULL&&p->next->next!=NULL){
ListNode *pre=p,*temp;
p=p->next;
temp=p->next;
p->next=temp->next;
temp->next=p;
pre->next=temp;
}else{
break;
}
}
return res->next;
}
};

Leetcode0024--Swap Nodes in Pairs 链表配对交换的更多相关文章

  1. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  2. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  3. [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点

    Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...

  4. 【LeetCode】Swap Nodes in Pairs(两两交换链表中的节点)

    这是LeetCode里的第24题. 题目要求: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定1->2->3->4, 你应该返回2->1->4- ...

  5. Leetcode24.Swap Nodes in Pairs两两交换链表中的节点

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...

  6. LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)

    题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...

  7. 24. Swap Nodes in Pairs 链表每2个点翻转一次

    [抄题]: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2 ...

  8. [Leetcode] Swap nodes in pairs 成对交换结点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...

  9. [LeetCode]Swap Nodes in Pairs 成对交换

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

随机推荐

  1. UVA1001 Say Cheese(Dijkstra或Floyd)

    题目链接:UVA1001 题意:在一个巨大奶酪中的A要以最短的时间与B相遇.在奶酪中走一米的距离花费的时间是10s,而奶酪中有许多洞,穿过这些洞的时间是0s.给出A.B以及各个洞的坐标,求最短的时间. ...

  2. 爬虫文件存储-2:MongoDB

    1.连接MongoDB 连接 MongoDB 我们需要使用 PyMongo 库里面的 MongoClient,一般来说传入 MongoDB 的 IP 及端口即可,第一个参数为地址 host,第二个参数 ...

  3. 简单的SpringBoot环境搭建

    开始搭建前请确认您的计算机中的Maven已正确配置 一:使用IDEA创建一个Maven项目,图中第一个指针请选择自己正在使用的JDK版本,指针二请打勾,选中指针三所指向的类型并点击Next 二:填写G ...

  4. 【codeforces 796C】Bank Hacking(用一些技巧来代替multiset)

    [题目链接]:http://codeforces.com/contest/796/problem/C [题意] 给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被 打 ...

  5. Java基础学习总结(71)——深入理解Java虚拟机内存

    Java虚拟机中的内存分配图 : 各个区域的特性总结如下表: 补充说明: 当多线程情形下,可能多个线程要在堆上分配内存,那么可能出现内存分配的同步问题,解决方案有两个,一个就是同步内存分配动作:另一个 ...

  6. linux -- 视频尺寸-cif、2cif、dcif、D1、HD1、4D1

    1 CIF简介     CIF是常用的标准化图像格式(Common Intermediate Format).在H.323协议簇中,规定了视频采集设备的标准采集分辨率.CIF = 352×288像素 ...

  7. 027依据前序遍历和中序遍历,重建二叉树(keep it up)

    剑指offer中题目:http://ac.jobdu.com/problem.php?pid=1385 题目描写叙述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.如果输入的前序遍历和中 ...

  8. android studio配置android开发环境

    1.下载安装android-studio-bundle 地址:https://developer.android.com/sdk/index.html 注意:指定android sdk和android ...

  9. C++开发人脸性别识别总结

    历时一个月,最终在昨天把<C++开发人脸性别识别总结>系列博客完毕了,第一篇博客发表在2015年12月29日,截止昨天2016年2月29日最后一篇完毕,去除中间一个月的寒假,正好一个月,首 ...

  10. jenkins+jmeter+ant+jmeter在Jenkins上报告

    1.jmeter+ant 参考 http://www.cnblogs.com/dieyaxianju/p/8268802.html 2.在jenkins上配置 3.执行成功 4.配置报告  参考 下载 ...