LeetCode - 24. Swap Nodes in Pairs
发布日期:2021-08-24 05:20:27 浏览次数:2 分类:技术文章

本文共 1394 字,大约阅读时间需要 4 分钟。

 24. Swap Nodes in Pairs

 ----------------------------------------------------------------------------

Mean: 

给定一个链表,交换这个链表两两相邻的元素.

analyse:

Time complexity: O(N)

 

view code

/**
* -----------------------------------------------------------------
* 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;
}
/*
*/

转载于:https://www.cnblogs.com/crazyacking/p/5200178.html

转载地址:https://blog.csdn.net/weixin_33811539/article/details/93423070 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:关闭 Scroll Lock
下一篇:jsf常见错误(收藏)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年03月29日 23时46分15秒