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.

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head)
{
struct ListNode *p=head,*q=head; int LiskLen=; while(q!=NULL)
{
LiskLen++;
q=q->next;
}
if(LiskLen%==)
{
while(p!=NULL)
{
int temp;
temp=p->val;
p->val=p->next->val;
p->next->val=temp; p=p->next->next;
}
}
else
{
while(p->next!=NULL)
{
int temp;
temp=p->val;
p->val=p->next->val;
p->next->val=temp; p=p->next->next;
}
} return head;
}

LeeCode-Swap Nodes in Pairs的更多相关文章

  1. Leetcode-24 Swap Nodes in Pairs

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

  2. 24. Swap Nodes in Pairs

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

  3. [LintCode] Swap Nodes in Pairs 成对交换节点

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

  4. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

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

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

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

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

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

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

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

  10. 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. CF 579A Raising Bacteria

    题意:细菌爱好者在盒子里面培养细菌.最初盒子是空的,每天可以往盒子里面添加任意数目的细菌,而且每天每个细菌会分裂成两个.在某一时刻要想获得某一数量的细菌,要求最初放置的最少的细菌数目. 思路: 求出 ...

  2. Game of Life 解答

    Question According to the Wikipedia's article: "The Game of Life, also known simply as Life, is ...

  3. linux下添加中文输入法

    一.安装环境 查看linux版本号 [ztteng@ztteng ~]$ lsb_release -aLSB Version:    :core-4.0-ia32:core-4.0-noarch:gr ...

  4. Yii2 分页类的扩展和listview引用

    Yii2 本身提供了不错分页选项供用户设置,但是实际项目中我们往往需要复杂一些的分页样式,例如下图所示的效果,上下翻页可用和不可用均用图标来替换.

  5. python高级编程之装饰器04

    from __future__ import with_statement # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrat ...

  6. 在Unity3d编辑器中加入菜单以及菜单项

    在引用UZGUI插件时,u3d编辑器的菜单条发生了变化,新增了菜单和菜单项,于是乎自己也像尝试一下,看了EZGUI的About_EZ_GUI脚本文件后,结果大出我所料,原来SO EASY! using ...

  7. eclipse中多个工程编译到同一个目录下

    1.点击link source  2.选择Java(ps:Java文件目录)或者resource(ps:配置文件目录)  3.最后结果,然后使用project中的clean进行编译,就可以把两个工程编 ...

  8. Emacs颜色设置

    1.下载color-theme主题包 下载链接:http://download.savannah.gnu.org/releases/color-theme/ color-theme-6.6.0.zip ...

  9. hdu3599 War(最大流)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud War Time Limit: 2000/1000 MS (Java/Others ...

  10. hadoop官网介绍及如何下载hadoop(2.4)各个版本与查看hadoop API介绍

    1.如何访问hadoop官网?2.如何下载hadoop各个版本?3.如何查看hadoop API? 很多同学开发都没有二手资料,原因很简单觉得不会英语,但是其实作为软件行业,多多少少大家会英语的,但是 ...