LeetCode - 24. Swap Nodes in Pairs
2016-02-19 10:52
549 查看
24. Swap Nodes in Pairs
Problem's Link
----------------------------------------------------------------------------[b]Mean:[/b]
给定一个链表,交换这个链表两两相邻的元素.
[b]analyse:[/b]
略
[b]Time complexity: O(N)[/b]
[b]view code[/b]
/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Date : 2016-02-19-10.35
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
// 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)
{
if(!head || head->next==nullptr)
return head;
ListNode *frontPtr=head,*backPtr=head->next;
while(frontPtr && backPtr)
{
swap(frontPtr->val,backPtr->val);
frontPtr=frontPtr->next;
if(frontPtr!=nullptr)
frontPtr=frontPtr->next;
backPtr=backPtr->next;
if(backPtr!=nullptr)
backPtr=backPtr->next;
}
return head;
}
};
int main()
{
return 0;
}
/*
*/
相关文章推荐
- namenode fsimage edits
- node查询mySql表
- windows下node.js环境配置
- [Nodejs] 记录一个小坑:Can't set headers after they are sent
- node递归属性目录结构
- node递归属性目录结构
- 我们为什么要看《超实用的Node.JS代码段》
- 五子棋(围棋) canvas node html5 js
- 我们为什么要看《超实用的Node.JS代码段》
- hadoop+zookepper实现namenode的高可用
- NodeJS 常用模块
- Node.JS 学习路线图
- node处理get请求
- 理解nodejs模块的scope
- Node.js之EventEmitter开发
- ubuntu14.04 安装nodejs步骤
- [LeetCode 024] Swap Nodes in Pairs
- LeetCode 116. Populating Next Right Pointers in Each Node
- win7安装nodejs
- node相关