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. 基于jQuery仿Flash横向切换焦点图

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

  2. 浏览器中F5和CTRL F5的行为区别

    前言 在印象中,浏览器中的F5和刷新按钮是一样的效果,都是对当前页面进行刷新:Ctrl-F5的行为也是刷新页面,但是会清除浏览器缓存,这在前端调试时候会常用.二者真正的区别是什么呢?在stackove ...

  3. EmWebAdmin 导航栏分析

    templates/gentelella/base.tpl <!DOCTYPE html> <html lang="en"> <!-- Smarty ...

  4. PHP——小尾巴之流程处理

    说明:首先新建一个流程,把处理流程的节点人员添加进去,最后点确定提交至数据库 处理流程:不同用户登录进去处理自己的节点部分对其审核通过 新建两个流程: 第一个为借款流程:处理顺序为:李四发起=> ...

  5. hdu6153 扩展kmp求一个字符串的后缀在另一个字符串出现的次数。

    /** 题目:hdu6153 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给定两个串,求其中一个串t的每个后缀在另一个串s中出现的次数乘以 ...

  6. 关于HTML5中hash部分API在历史管理,选项卡自动跳转中的一点使用

    在群里最近有人提到如何处理在页面跳转之后,想要直接定位到相对应的选项卡的选项上,比如首页有登录和注册两个按钮,而登录注册页面有一对选项卡分别对应的是登陆和注册,想要实现的功能是点击登陆去到登录页的登录 ...

  7. 对于try catch放在能够很好地处理例外的位置

    Exception有一个message属性.在使用catch的时候可以调用: Catch(IOException e){System.out.println(e.message())}; Catch( ...

  8. 嵌入式开发之davinci--- ccs 编译lib库

    (1)ccs 简介 (2)ccs 输出格式coff elf (3)ccs 编译lib 算法库 (4)ccs 存在的问题 ---------------------------------------- ...

  9. 阿里Java开发手冊之编程规约

    对于程序猿来说,编程规范能够养成良好的编程习惯,提高代码质量,减少沟通成本.就在2月9号,阿里出了一份Java开发手冊(正式版),分为编程规约.异常日志.MySQL规约,project规约.安全规约五 ...

  10. bootstrap基础学习六篇

    bootstrap按钮 类 描述 .btn 为按钮添加基本样式 .btn-default 默认/标准按钮 .btn-primary 原始按钮样式(未被操作) .btn-success 表示成功的动作 ...