【转载请注明】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. cmake更新版本简记

    问题描述: 由于需求,要在服务器上安装ANTs(Advanced Normalization Tools).然而最新版的ANTs需要下载源码并用cmake编译, 于是根据https://github. ...

  2. Win32 线程同步

    Win32 线程同步 ## Win32线程同步 ### 1. 原子锁 ### 2. 临界区 {全局变量} CRITICAL_SECTION CS = {0}; // 定义并初始化临界区结构体变量 {线 ...

  3. SprinMVC接收参数乱码解决篇

    1.Spring 默认的字符编码格式为iso-8859-1,为此Spring专门提供了字符过滤器org.springframework.web.filter.CharacterEncodingFilt ...

  4. MySQL-----一对多

    一对多: 用户表和部门表 用户: 用户id 用户名 部门 1 George   1 2 Elizabeth 1 3 Bruce 2 4 Catherine 3 部门: 部门id 部门名称 1 CEO ...

  5. Python网络爬虫之Scrapy框架(CrawlSpider)

    目录 Python网络爬虫之Scrapy框架(CrawlSpider) CrawlSpider使用 爬取糗事百科糗图板块的所有页码数据 Python网络爬虫之Scrapy框架(CrawlSpider) ...

  6. 第十三节:pandas之groupby()分组

    1.Series()对象分组 1.1.单级索引 1.2.多级索引 2.DataFrame()对象分组 3.获取一个分组,遍历分组,filter过滤.

  7. 深入浅出了解HTTP协议

    深入浅出了解HTTP协议

  8. 【[Offer收割]编程练习赛12 A】歌德巴赫猜想

    [题目链接]:http://hihocoder.com/problemset/problem/1493 [题意] [题解] 枚举P从2..n/2 如果P是质数且N-P也是质数; 则输出P和N-P就好; ...

  9. MG loves string

    MG loves string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others ...

  10. 混合了RBAC和ACL的权限系统(一) -- 用户组织结构

    最近的工作是一个基础设计,打造一个基于RBAC和ACL的权限基础组件. 这个基础组件的特点是:同时混合了RBAC和ACL的认证方式,也就是说同时提供系统级别的授权(RBAC)和对象级别的授权(ACL) ...