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. 最短路径问题:dijkstar

    算法描述: 输入图G,源点v0,输出源点到各点的最短距离D 中间变量v0保存当前已经处理到的顶点集合,v1保存剩余的集合 1.初始化v1,D 2.计算v0到v1各点的最短距离,保存到D for eac ...

  2. 理解 Linux 配置文件分类和使用

    理解 Linux 配置文件分类和使用 本文说明了 Linux 系统的配置文件,在多用户.多任务环境中,配置文件控制用户权限.系统应用程序.守护进程.服务和其它管理任务.这些任务包括管理用户帐号.分配磁 ...

  3. IO之内核buffer----"buffer cache"

    举例 一般情况下,Read,write系统调用并不直接访问磁盘.这两个系统调用仅仅是在用户空间和内核空间的buffer之间传递目标数据. 举个例子,下面的write系统调用仅仅是把3个字节从用户空间拷 ...

  4. 《Java程序员面试笔试宝典》之Java变量命名有哪些规则

    在Java语言中,变量名.函数名.数组名统称为标识符,Java语言规定标识符只能由字母(a~z,A~Z).数字(0~9).下划线(_)和$组成,并且标识符的第一个字符必须是字母.下划线或$.此外,标识 ...

  5. Traceroute原理介绍

    一.路由追踪 路由跟踪,就是获取从主机A到达目标主机B这个过程中所有需要经过的路由设备的转发接口IP. 二.ICMP协议 Internet控制报文协议(internet control message ...

  6. java与.net比较学习系列(4) 运算符和表达式

    上一篇总结了java的数据类型,得到了冰麟轻武等兄弟的支持,他们提出并补充了非常好的建议,在这里向他们表示感谢.在后面的文章中,我会尽力写得更准确和更完善的,加油! 另外,因为C#是在java之后,也 ...

  7. [React] Styling a React button component with Radium

    React's inline styles allow components to stand on their own by not requiring any external CSS. Howe ...

  8. [Git] --no-verify

    Somtimes, the project might set the commit message guide line, if your commit doesn't meet the requi ...

  9. form表单提交校验

    <form id="myForm" action="http://www.365mini.com" method="post"> ...

  10. Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was made to load a program with an incorrect format.

    I have installed a Web application on IIS 7.0 windows server 2008 R2 64 bit OS I am refering a oracl ...