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. HDU1394 Minimum Inversion Number(线段树OR归并排序)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  2. 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)

    简单的暴力筛法就可. #include <iostream> #include <cstring> #include <cmath> #include <cc ...

  3. UGUI 快捷键创建UGUI组件

      使用NGUI的时候还有xxx快捷键创建, spirte,label,button等等. 在UGUI里面的时候好像是没有快捷键的. 不知道以后多久才能有这个功能.  在家里闲无聊的时候写了一个脚本, ...

  4. Unity 白猫操作小实例

    最近师兄找我说白猫的操作如何做,  0.0 结果白猫没有android的客户端玩不了,看了下视频介绍就简单做了下 效果图:   核心代码: using UnityEngine; using Syste ...

  5. OpenReports中文支持方案

    此文章在<OpenReports中文支持完全解决方案.doc>的基础上做优化,并贴出代码.已测试通过. 一.主要解决的问题 1 页面显示支持中文 2 与服务器或数据库的交互支持中文 3 查 ...

  6. c++面试知识点

    static #include<stdio.h> #include<iostream> #include<assert.h> using namespace std ...

  7. poj1703--Find them, Catch them(并查集应用)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32073   Accepted: ...

  8. hdu1171 Big Event in HDU 01-背包

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171 Problem ...

  9. [SQL学习笔记][用exists代替全称量词 ]

    学习sql的必经问题. 学生表student (id学号 Sname姓名 Sdept所在系) 课程表Course (crscode课程号 name课程名) 学生选课表transcript (studi ...

  10. angular的数据双向绑定秘密

    Angular用户都想知道数据绑定是怎么实现的.你可能会看到各种各样的词汇:$watch,$apply,$digest,dirty-checking... 它们是什么?它们是如何工作的呢?这里我想回答 ...