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.

code :

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(head == NULL)
return NULL;
if(head->next == NULL)
return head; ListNode *p = head;
ListNode *q = head->next;
ListNode *pre = NULL;
while( q != NULL && q->next != NULL) // p,q 指向相邻结点一前一后
{ // 注意奇数个结点的情况,判空为第一种
ListNode *tmp = q->next;
q->next = p;
p->next = tmp;
if( pre == NULL)
{
head = q;
pre = p;
}
else
{
pre->next = q;
pre = p;
}
p = p->next;
q = p->next;
}
if(pre == NULL) //只有两个结点
{
head = q;
q->next = p;
p->next = NULL;
return head;
}
if(q == NULL) //奇数个结点
{
return head;
}
q->next = p; //偶数个结点
p->next = NULL;
pre->next = q;
return head; }
};

【LeetCode】Swap Nodes in Pairs的更多相关文章

  1. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  2. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  3. 【leetcode】Swap Nodes in Pairs (middle)

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

  4. 【LeetCode】Swap Nodes in Pairs(两两交换链表中的节点)

    这是LeetCode里的第24题. 题目要求: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定1->2->3->4, 你应该返回2->1->4- ...

  5. 【Leetcode】【Medium】Swap Nodes in Pairs

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

  6. 【链表】Swap Nodes in Pairs(三指针)

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

  7. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

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

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

  9. leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现

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

随机推荐

  1. Codeforces 543B Destroying Roads(最短路)

    题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...

  2. jQuery--引入,基本语法,以及常用事件

    一.初识jQuery jQuery是一个JavaScript函数库.主要包含的功能有:HTML元素的选取.操作,CSS操作,HTML事件函数,JavaScript特效和动画,HTML DOM遍历和修改 ...

  3. js 去除字符串开头或者前几个字符。slice 也可以用于截取某一部分

    摘草自w3 slice() 方法可从已有的数组中返回选定的元素. 语法 arrayObject.slice(start,end) 参数 描述 start 必需.规定从何处开始选取.如果是负数,那么它规 ...

  4. 生成四位随机数的PHP代码

    纯数字的四位随机数 rand(1000,9999) 数字和字符混搭的四位随机字符串: function GetRandStr($len) { $chars = array( "a" ...

  5. 关于Redis的知识汇总[转]

    1. Overview 1.1 资料 <The Little Redis Book> ,最好的入门小册子,可以先于一切文档之前看,免费. 作者Antirez的博客,Antirez维护的Re ...

  6. Plan-9效应:为什么东西不坏就不要去修它

    http://www.aqee.net/the-plan-9-effect-or-why-you-should-not-fix-it-if-it-aint-broken/ Plan-9是一个很棒的.很 ...

  7. win7如何开启和关闭超级管理员账户

    激活命令: net user administrator /active:yes 关闭命令: net user administrator /active:no

  8. 如何更好辨认House of hello恶搞包的真假

    相信很多朋友都知道houseofhello恶搞包这个品牌,甚至很多朋友都买过,首先呢,她是恶搞包,算自主品牌,它无淘宝店,更没有所谓的香港实体店.因为这品牌受到广大朋友的狂热,导致无数仿品的出现,淘宝 ...

  9. 【Java】如何访问服务器

    HTTP协议---------->GET.POST.XMLHttpRequest TCP/IP协议 SOAP协议---------->Web Service Server的作用是处理HTT ...

  10. unity3d中的http通信

    转载 http://blog.csdn.net/mfc11/article/details/8188785的博客,如果侵权,请留言我及时删除! 前言 Unity3d 是一个跨平台的引擎,在移动互联网浪 ...