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.

改变指针:

if (!head || !(head->next) )    return head;
ListNode *cur = head,*next = head->next;
head = head->next;
while(next){
    next = next->next;//next->3
    cur->next->next =cur;//2->1
    if(!next || !(next->next)) {
      cur->next = next;//1->3;
      return head;  }
    cur->next = next->next;//否则 1->4
    cur = next;// cur ->3
    next = next->next;//next->4
}
return head;

只改变值:

ListNode* swapPairs(ListNode* head)
{
    if(!head) return nullptr;
    ListNode *frontPtr=head,*backPtr=head->next;
    while(frontPtr && backPtr){     swap(frontPtr->val,backPtr->val);
        frontPtr=frontPtr->next;
        if(frontPtr!=nullptr)
            frontPtr=frontPtr->next;
        backPtr=backPtr->next;
        if(backPtr!=nullptr)
            backPtr=backPtr->next;
    }
    return head;
}

递归:

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head == NULL||head->next==NULL) return head;
        ListNode* next = head->next;
        head->next = swapPairs(next->next);
        next->next = head;
        return next;
    }
};

练习—单链表—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】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 ☆☆☆(链表,相邻两节点交换)

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

  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

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

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

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

随机推荐

  1. Say bye to CMake and Makefile

    用了几年的CMake,最近想试着琢磨如何将C++应用的动态链接全部改成静态链接,发现还需要研究CMake的用法,进入CMake的文档, http://www.cmake.org/cmake/help/ ...

  2. bootstrap 模版

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...

  3. python 解决递归调用栈溢出

    递归函数 2578次阅读 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact ...

  4. 性能测试 - some

    1.日常业务中测试过的最大并发数,其QPS为多少? 答: 从服务端开发处得知线上某台机器单接口访问量为63万 因该接口为视频类访问接口,大多数接口集中于晚间时段.可计算QPS = 63万/8/3600 ...

  5. 解决gradle:download特别慢的问题

    使用AndroidStudio 2.2.2 新增加了一个dependencies,需要下载jar包,此时就会卡在 gradle:download https://….. 这个状态中. 原因就是因为我们 ...

  6. AFNetworking 使用总结 (用法+JSON解析)

    « AFNetworking 图片的本地缓存问题 Get application bundle seed ID in iOS » AFNetworking 使用总结 (用法+JSON解析)    Fr ...

  7. 瞬态抑制二极管TVS的基本知识

    二极管是大家熟悉的元件,但瞬态抑制二极管就可能不太熟悉了.本文将介绍这种特殊二极管的用途. 工作原理等基本知识.各种电子设备中的各种半导体器件,一般都在直流低电压范围各作;如果在电源中串入一个瞬间上百 ...

  8. 有关WCF的契约问题

    WCF中的契约包括4种 数据契约 DataContract ->DataMember 服务契约 ServiceContract-> OperactionContract 消息契约 Mess ...

  9. 用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应

    http://blog.csdn.net/panjunbiao/article/details/8615880   用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应

  10. cocos2dx lua 加密

    cocos2dx-lua项目发布时,为了保护lua源码,需要对lua进行加密.通常分为两种方式:加密文件和编译为字节码. 1.加密文件 前提是你不用luajit,而使用lua.这样这种方法是真正加密, ...