【转载请注明】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. Oracle学习总结(5)—— SQL语句经典案例

    --0.所有员工信息 SELECT * FROM emp --1.选择部门30的所有员工 SELECT * FROM emp WHERE deptno=20 --2.列出所有办事员(CLERK)的姓名 ...

  2. 正则表达式,字符串中需要两个反斜杠“\\d”

    这个正则表达式为什么会有两个反斜杠? "^.*?\\.(jpg|png|bmp|gif)$"上面这个正则表达式为什么有两个反斜杠呢?反斜杠点\.就能表示点.了,为什么还要在\.前面 ...

  3. 前台JSON字符串,spring mvc controller也接收字符串

    前台JSON字符串,spring mvc controller也接收字符串 前台: $.post(url, { data : JSON.stringify(obj) }, function(data) ...

  4. 迅为4412开发板Linux驱动教程/硬件知识及原理图的使用

    视频教程下载地址:http://pan.baidu.com/s/1pJwxUfL 嵌入式研发流程介绍 • PCB研发流程介绍 – 方案,原理图(网表) – layoutproject师(gerber文 ...

  5. spring基于通用Dao的多数据源配置

    有时候在一个项目中会连接多个数据库,须要在spring中配置多个数据源,近期就遇到了这个问题,因为我的项目之前是基于通用Dao的,配置的时候问题不断.这样的方式和资源文件冲突:扫描映射文件的话,Sql ...

  6. 王立平--Failed to pull selection

    解决的方法:重新启动eclipse

  7. Cocoa pods的安装和使用

    现在网上关于cocoapods的安装使用资料有很多,有些方法能用,有些是用不了的,别问为什么,因为我就是从坑里走出来的.在此自己整理了一些方法: 一般需要先升级Ruby环境: 第一步:安装rvm $  ...

  8. C++操作Json字符串

    一.从字符串中读取JSON a.cpp ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  9. hdu1325 Is It A Tree?(二叉树的推断)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  10. POJ2417 Baby-Step-Gaint-Step 算法

    考虑一个问题:A^x%p=B,给定A,B,p,求x的最小非负整数解. 在p是质数的情况下,这个问题比較简单. A^x=B(mod P) (P is a Prime, A,B<P) Let m = ...