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.

分析:题意为给予一个链表,交换相邻的两个节点然后返回头结点。要求算法空间复杂度为O(1).

思路:新建一个链表每次插入temp->next后再插入temp,注意要判断若最后只剩下一个节点则不需要交换,每次交换了节点要把尾节点的下一个指向空。

代码:

/**
* 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==NULL || head->next==NULL) return head;
ListNode *helper=new ListNode(0);
ListNode *temp=head;
ListNode *cur=helper;
while(temp && temp->next)
{
ListNode *next=temp->next->next;
cur->next=temp->next;
cur=cur->next;
cur->next=temp;
cur=cur->next;
cur->next=NULL;
temp=next;
}
if(temp) cur->next=temp;
return helper->next;
}
};

  

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 024 Swap Nodes in Pairs

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

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

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

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

  5. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  6. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  7. [LeetCode 题解]:Swap Nodes in Pairs

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a li ...

  8. LeetCode OJ:Swap Nodes in Pairs(成对交换节点)

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

  9. No.024:Swap Nodes in Pairs

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

随机推荐

  1. NYOJ-655 光棍的YY AC 分类: NYOJ 2013-12-29 19:24 224人阅读 评论(0) 收藏

    #include<stdio.h> #include<string.h> char str[210]; int max[210][52]={0}; int sum(int n, ...

  2. 用npm安装express后express命令找不到

    Windows 平台加了 npm install -g express 也不行AppData\Roaming\npm 下面没有 express.bat 解决办法: sudo npm install - ...

  3. vml 在IE8 不显示的问题, Group不能用等问题.

    IE8 不显示的问题: <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> ...

  4. img图片底部出现莫名的下边距问题

    谷歌中这样是解释的: 图片底部的空隙实际上涉及行内元素的布局模型,图片默认的垂直对齐方式是基线,而基线的位置是与字体相关的.所以在某些时候,图片底部的空隙可能是 2px,而有时可能是 4px 或更多. ...

  5. [翻译]The Neophyte's Guide to Scala Part 12: Type Classes

    The Neophyte's Guide to Scala Part 12: Type Classes 过去的两周我们讨论了一些使我们保持DRY和灵活性的函数式编程技术,特别是函数组合,partial ...

  6. Sqli-labs less 23

    第二部分/page-2 Advanced injection Less-23 Sql语句为$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1 ...

  7. 使用tomcat7创建异步servlet

    该篇文章翻译自:http://developerlife.com/tutorials/?p=1437 一.简介 Servlet API 3.0 之前,需要使用类似Comet的方式来实现创建异步的Ser ...

  8. jquery mobile的学习资料

    磨刀不误砍柴工!想要学的快就得有好的资源.jquery mobile只是jquery的一个插件,所以相对简单易学.只要有jquery的基础就好.如果想修改东西的话,那么需要的知识就相对较多了. 书 & ...

  9. hdu 3271 SNIBB 数位DP+二分

    思路:dp[i][j]:表示第i位在B进制下数字和. 用二分找第k个数! 代码如下: #include<iostream> #include<stdio.h> #include ...

  10. 标准管道(popen)

    NAME popen, pclose - pipe stream to or from a process SYNOPSIS #include <stdio.h> FILE *popen( ...