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;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(NULL == head) return head;
ListNode *pre,*p,*q;
pre = NULL; p = head; q = head->next;
int i = 0;
while(q){
if(i%2 == 0){
if(pre == NULL){
pre = q;
head = q;
}else
pre->next = q;
p->next = q->next;
q->next = p;
// move to the next;
i = 0;
q = p->next;
i++;
}else{
pre = p;
p = q;
q = q->next;
i++;
}
}
return head;
}
};

  

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

随机推荐

  1. 组合索引leaf 数据存储

    1 Z 2 X 3 U 4 T 5 G 6 F 7 C 8 B 9 A 1 A 2 B 3 C 4 D Oracle的索引是以平衡树的方式组织存储的:保存的是索引列的值,以及该行的rowid的一部分( ...

  2. HDOJ(HDU) 1678 Shopaholic

    Problem Description Lindsay is a shopaholic. Whenever there is a discount of the kind where you can ...

  3. HDOJ(HDU) 1408 盐水的故事

    Problem Description 挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下:然后滴二滴,停一下:再滴三滴,停一下-,现在有一个问题:这瓶盐水一共有VUL毫升,每一滴是D毫升,每一滴的速 ...

  4. 非常华丽的jQuery/HTML5应用推荐

    jQuery确实是一个非常优秀的JavaScript框架,尤其是结合HTML5,更可以让网页变得生动有趣.今天向大家推荐一些非常华丽的jQuery/HTML5应用,废话不多说,直接看吧. 1.jQue ...

  5. C++中的string类(1)

    http://blog.sina.com.cn/s/blog_51409e8f01009h7g.html 前言: string 的角色1 string 使用1.1 充分使用string 操作符1.2 ...

  6. SRM 585 DIV1

    A 树形dp是看起来比较靠谱的做法 , 但是转移的时候不全面就会出错 , 从贪心的角度出发 , 首先让第一量车走最长路, 然后就会发现递归结构 , 得到递归式 f[i] = ( f[i-2] + f[ ...

  7. Linux 常用命令记录

    1.查看磁盘空间使用情况 df -[a i m] 或更多 df -lh 2.查看目录文件占用大小 du -sh * du --max-depth=1 -lh 3.内存使用qingkuang free ...

  8. Ajax请求用户控件(.ascx)404错误

  9. [Angular 2] Interpolation: check object exists

    In Angular2, sometime we use @Output to pass data to parent component, then parent may pass the data ...

  10. [Redux] React Todo List Example (Adding a Todo)

    Learn how to create a React todo list application using the reducers we wrote before. /** * A reduce ...