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.

ListNode *swapPairs(ListNode *head)
{
if(head==NULL) return head;
int flag = 0;
ListNode *p = head;
ListNode *q = p->next;
while(q!=NULL)
{
p->next = q->next;
q->next = p;
if(flag==0)//第一次操作时保存头指针
{
head = q;
}
flag = 1;
ListNode *pre = p;//记录P
//p后移,q指向p的后继,如果p==NULL,p=p->next;报错,此时只需要直接
//将q赋NULL
p = p->next;
if(p!=NULL)
q = p->next;
else
q = NULL;
//若前面的p=p->next使得q==NULL,就不对了
if(q!=NULL)
pre->next = q;
}
return head;
}

关于链表的操作比较麻烦,虽然不会涉及到动态规划、搜索等复杂的算法思想,但是指针操作特别容易出错,面试或者笔试时,不容易准确快速的写出没有bug的代码,唯有平时好好总结,没事多看几遍啦。。。

LeetCode_链表操作1—Swap Nodes in Pairs的更多相关文章

  1. Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转

    问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...

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

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

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

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

  4. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  5. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  6. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

    Swap Nodes in Pairs  Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  7. LeetCode: Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  8. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

  9. leetcode-algorithms-24 Swap Nodes in Pairs

    leetcode-algorithms-24 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and re ...

随机推荐

  1. 在sql结果中显示行号

    1.准备 create table newtable ( name ), ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ); ); ); ); ); 2.实现 ) a ...

  2. Java编译后产生class文件的命名规则

    今天刚好有同学问了下Java编译后产生的.class文件名的问题,虽然一直都在使用Java做开发,但是之前对编译后产生的.class文件名的规范也基本没做了解过,也真的是忏愧啊!今天无论如何都要总结下 ...

  3. MVC 使用IOC实现

    实现步骤: 1. 实现IDependencyResolver接口并通过DependencyResolver.SetResolver告知MVC,将部分类型实例解析工作交由IoC容器来处理: using ...

  4. 关于JDK环境变量的配置问题

    网上配置JDK环境变量的时候一直说要配置三个环境变量,什么JAVA_HOME,Path,CLASSPATH 其实是说以后如果要修改JDK的版本或者路径,只要更改JAVA_HOME就可以了,Path,C ...

  5. 无偏估计(Unbiased Estimator)

    无偏估计是参数的样本估计量的期望值等于参数的真实值. 一个简单的例子(https://www.zhihu.com/question/22983179/answer/23470969): 比如我要对某个 ...

  6. imx6 uboot logo 更改

    最近需要更改im6 uboot的开机logo,使用10.1inch, 1024x600,18bit的LCD,期间遇到了很多的问题,记录于此. 参考链接 https://community.nxp.co ...

  7. Spring Boot简化了基于Spring的应用开发

    Spring Boot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的.产品级别的Spring应用. Spring Boot为Spring平台及第三方库提供开箱即用的设置,这样你就可 ...

  8. js数字格式化(加千分位逗号)

    需求:当金额大于10000时,在作展示的时候,需要加千分位逗号,就是每隔1000要用逗号分隔: 方法一:使用toLocaleString()方法 此方法和toString()方法的区别看这里 < ...

  9. [NOIP 2014复习]第二章:搜索

    一.深度优先搜索(DFS) 1.Wikioi 1066引水入城 题目描写叙述 Description 在一个遥远的国度,一側是风景秀美的湖泊,还有一側则是漫无边际的沙漠.该国的行政 区划十分特殊,刚好 ...

  10. SQL命令优化(积累)

    与数据库交互的基本语言是sql,数据库每次解析和执行sql语句多需要执行很多步骤.以sql server为例,当数据库收到一条查询语句时,语法分析器会扫描sql语句并将其分成逻辑单元(如关键词.表达式 ...