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.

Subscribe to see which companies asked this question

解答:
这道题目的话就是区分奇数个节点和偶数个节点,然后注意一下只有一个头节点、空链表的情况就好了。
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* swapPairs(struct ListNode* head) {
    struct ListNode *tmp, *new_head = head, *tail = NULL;

    while(NULL != head&&NULL != head->next){
        tmp = head->next;
        head->next = head->next->next;
        tmp->next = head;
        if(head == new_head){
            new_head = tmp;
        }
        else{
            tail->next = tmp;
        }
        tail = head;
        head = head->next;
    }
    if(NULL != head&&NULL != tail){
        tail->next = head;
    }
    return new_head;
}

LeetCode OJ 24. Swap Nodes in Pairs的更多相关文章

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

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

  2. 【LeetCode】24. Swap Nodes in Pairs (3 solutions)

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

  3. 【一天一道LeetCode】#24. Swap Nodes in Pairs

    一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  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. 【LeetCode OJ】Swap Nodes in Pairs

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

  6. LeetCode OJ:Swap Nodes in Pairs(成对交换节点)

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

  7. LeetCode:24. Swap Nodes in Pairs(Medium)

    1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...

  8. 24. Swap Nodes in Pairs

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

  9. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

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

随机推荐

  1. delphi EncdDecd.pas单元中Encoding方法出现#$D#$A的解决方法

    例如: s:= 'http://detail.tmall.com/item.htm?id=45545826531&abbucket=_AB-M129_B17&acm=03130.100 ...

  2. C#语言正则用法

    string phone =""; string pattern @"|\d{10}"; bool rusurt = false; Console.WriteL ...

  3. 基于Linux命令行KVM虚拟机的安装配置与基本使用

    背景 由于生产环境的服务器并不会安装桌面环境,简单操作的图形化安装也不适合批量部署安装.因此,我还是更倾向于在命令下安装配置KVM虚拟机.结合了一些资料和个人使用的状况,我大致列出了一些基本和常用的使 ...

  4. redis参数改进建议

    1.修改stop-writes-on-bgsave-error为no当前配置为yes,分别修改redis.conf和当前实例#redis.confstop-writes-on-bgsave-error ...

  5. Struts2学习:struts.xml引入自定义的xml文件

    随着项目代码的增多,用一个struts.xml来管理所有功能模块的Action未免显得臃肿且结构不清晰,因此可以根据实际的功能划分,将各模块的Action放在自定义的xml文件中,再引入struts. ...

  6. elastalert新增自定义警告推送

    举例,博主公司有自己的内部通讯工具(类似QQ),接下来用IM代称该工具.于是希望elastalert的警告推送可以支持IM的公众号群发功能. 等博主这个月知识库写了再来补充hah

  7. day24类的继承

    类的继承1 什么是继承    继承一种新建类的方式,新建的类称之为子类/派生类,被继承的类称之为父类\基类\超类    python中继承的特点:        1. 子类可以遗传/重用父类的属性   ...

  8. C# WPF 文件复制,相对路径

    /// <summary> /// 下载/复制 /// </summary> /// <param name="sender"></par ...

  9. C# .NET 配置404,500等错误信息

    <customErrors mode="On" defaultRedirect="viewAll.html"><!--所有的错误显示页--&g ...

  10. 完全使用css编写复选框

    在日常的项目中,出现复选框或者单选框,应该都属于常见需求了,最开始阶段,一般只有两种可能性: 一.完全使用  <input type="checkbox" />或者&l ...