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. CSS阻止文本选择

    在日常运用中,经常遇到点击按钮/菜单的时候,选中了文本,为了避免这种情况,可以使用纯css来解决这个问题(IE10+),对于旧版本的就只能用js:onselectstart = 'return fal ...

  2. C++基于模板顺序表的实现(带排序)

    说明:代码是可以运行的,但是发表在博客上后复制到编译器里面报N多错误,找了半天原因是网页里面生成了一些空白字符,这些字符编译器无法识别. 因此使用了2种插入格式插入代码. 第二个带注释解释的代码不可复 ...

  3. php中urlencode使用

    URLEncode的方式一般有两种,一种是传统的基于GB2312的Encode(Baidu.Yisou等使用),另一种是基于UTF-8的Encode(Google.Yahoo等使用). 本工具分别实现 ...

  4. ida idc函数列表全集

    下面是函数描述信息中的约定: 'ea' 线性地址 'success' 0表示函数失败:反之为1 'void'表示函数返回的是没有意义的值(总是0) AddBptEx AddBpt AddCodeXre ...

  5. android studio gradle自动签名构建实现

    我为自己代言: 一.在android studio中生成签名文件. 1.在android studio 选中项目,在菜单栏中选择Build. 2.点击Generate Signed APK选项卡. 3 ...

  6. java练习-滚动文字

    <marquee direction="left" onMouseOver="this.scrollAmount=5" onMouseOut=" ...

  7. Oracle AWR

    http://www.linuxidc.com/Linux/2011-10/44563.htm http://t.askmaclean.com/thread-3227-1-1.html http:// ...

  8. cf B. Petya and Staircases

    http://codeforces.com/contest/362/problem/B 先排序,然后判断第一个和最后一个是不是脏的,如果是则输出NO,然后判断其中三个脏的是不是连着的,如果是也输出NO ...

  9. windows系统各版本 各种数据结构

    极爽啊http://msdn.moonsols.com/

  10. Tracing JIT

    在一个从Java源码编译到JVM字节码的编译器(如javac.ECJ)里,一个“编译单元”(CompilationUnit)指的是一个Java源文件.而在Dalvik VM的JIT里也有一个结构体名为 ...