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.(头二个节点已经事先处理)找出要换位置的两个节点的前驱节点,设为tempHead,设要更换的两个节点为p,q

2.那么直接p节点拿出,tempHead->next=q;p->next=NULL;

3.然后就是普通把p几点插入到q节点后面即可。p->next=q->next;q->next=p;

4.最后把暂时头部节点后移两个位置,进行下一次更换。

图解如下:

代码:

class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* res;
if(head==NULL) return NULL;
if(head->next==NULL) return head;
//交换第一,二个节点
ListNode* first=head->next;
head->next=first->next;
first->next=head; ListNode* tempHead=first->next;
ListNode* p;
ListNode* q;
while (tempHead)
{
if(tempHead->next!=NULL&&tempHead->next->next!=NULL){
p=tempHead->next;
q=tempHead->next->next;
}
else return first; tempHead->next=q;
p->next=NULL;//中间节点插入即可 p->next=q->next;
q->next=p;
tempHead=q->next; }
return first;
}
};

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. 24. Swap Nodes in Pairs 链表每2个点翻转一次

    [抄题]: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2 ...

  4. leetcode 24. Swap Nodes in Pairs(链表)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  5. 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 算法思想:基本操作就是链表 ...

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

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

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

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

  8. 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 ...

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

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

  10. Leetcode 线性表 Swap Nodes in Pairs

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

随机推荐

  1. CSS3 按钮特效(一)

    1. 实例 2.HTML 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  2. CentOS安装使用vnc进行远程桌面登录

    以下介绍在CentOS 7下安装vncserver并使用vnc-viewer进行登录(使用root权限): 1.运行命令yum install tigervnc-server安装vncserver: ...

  3. com口操作excel

    _Application app;       //Excel应用程序接口 Workbooks books;        //工作薄集合 _Workbook book;     //工作薄 Work ...

  4. UIScrollView的contentSize、contentOffset和contentInset属性

    IOS中,UIScrollView是可以滚动的视图,其中最常用的UITableView就是继承了UIScrollView. 跟所有的view一样,UIScrollView有一个frame属性,同时,U ...

  5. 禁止浏览器static files缓存篇

    由于CSS/JS文件经常需要改动,前端调试时是不希望浏览器缓存这些文件的. Meta法 目前在chrome调试还没有遇到问题,好用!此方法假设浏览器是个好人!很听我们的话! <meta http ...

  6. [Python3网络爬虫开发实战] 3.1.1-发送请求

    使用urllib的request模块,我们可以方便地实现请求的发送并得到响应,本节就来看下它的具体用法. 1. urlopen() urllib.request模块提供了最基本的构造HTTP请求的方法 ...

  7. CentOS7安装Tomcat9并设置开机启动

    1.下载 Tomcat 9 CentOS 7 下创建目录并下载文件: cd /usr/local/ mkdir tomcat cd tomcat wget http://mirrors.hust.ed ...

  8. js 技巧 (十)广告JS代码效果大全 【2】

    2.[鼠标感应]     与前面一个代码不同的是,当鼠标移动到广告图片上是可以感应显示另外设置好的广告大图效果,下面就是实现效果所需代码: function bigshow(){     docume ...

  9. db2,差集

    --漏报的数据 FROM A LEFT JOIN A′ ON 交集的条件 WHERE A′.xx IS NULL --多报的数据 FROM A′ LEFT JOIN A ON 交集的条件 WHERE ...

  10. Linux学习笔记(一) 文件系统

    对于每一个 Linux 学习者来说,了解 Linux 文件系统的结构是十分有必要的 因为在 Linux 中一切皆文件,可以说只有深入了解 Linux 的文件系统,才会对 Linux 有更深刻的理解 L ...