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. matlab使用的心得

    保存变量到一个文件,可以是部分变量或者全部变量save('back.mat','a'):%把a变量保存在文件中 加载文件中的变量可以是部分或者全部变量load('matlab.mat','a') 向字 ...

  2. 基于jQuery仿Flash横向切换焦点图

    给各网友分享一款基于jQuery仿Flash横向切换焦点图.利用Flash可以制作很多漂亮的图片相册应用,今天我们要用jQuery来实现这样的效果.它是一款仿Flash的横向图片切换焦点图插件,可以自 ...

  3. Python中import和from import

    Python里面的import和from import都是用于导入一个模块,两者的区别是 如果你在使用某模块内函数时不想写模块名,那么就用from import方式导入,如果是用import方式就要写 ...

  4. linux - camera capture

    //cut a picture#include <stdio.h>#include <stdlib.h>#include <string.h>#include &l ...

  5. 关于javascript遍历对象

    1:遍历对象属性var obj={a:'aa',b:'bb'} for(var i in obj) { alert(i); //输出 a b } var obj={'a':'aa','b':'bb'} ...

  6. C++ 函数的扩展③--函数重载

    //函数扩展--函数重载(C语言不支持函数重载) #include<iostream> using namespace std; //函数重载在本质上是相互独立的不同函数(静态链编),在c ...

  7. C++ 匿名对象产生场景

    //匿名对象产生的三种场景 #include<iostream> using namespace std; class Point{ public: Point(int a,int b){ ...

  8. 『Spring.NET+NHibernate+泛型』框架搭建之DAO(三)★

    本节内容介绍Nhibernate所封装的数据库訪问层.只是我增加了泛型进行封装.大概思路:首先,我们有一个接口层,另一个相应的实现层.在接口层中我们先定义一个父接口,父接口中定义每个接口都可能会用到的 ...

  9. hdu 1201:18岁生日(水题,闰年)

    18岁生日 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  10. 复习及总结--.Net线程篇(3)

    不幸的发现,原来多线程的东西还有好多. 不只是一个Thread就能把事情做完的,好吧,孤陋寡闻了 这里总结下  复习及总结--.Net线程篇(2)里的两个概念AppDomain和ThreadPool ...