本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Swap Nodes in Pairs

Total Accepted: 12511 Total
Submissions: 39302

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.

题意:交换给定链表中的相邻节点,但不能够改变链表里的值

如1->2->3->4交换后为2->1->4->3

思路:

按题意中的扫描去改变每两个相邻节点的next指针的指向就可以。

小技巧:

由于处理每两个相邻节点的时候,须要一个指针记录它们前一个节点,而头节点前面没有节点,

所以可设置一个dummy节点指向头指针,这样开头的两个节点的处理方式跟其他的相邻节点的处理方式就一样了

复杂度:时间O(n),空间O(1)

/**
* 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) return NULL;
ListNode *dummy = new ListNode(0); dummy->next = head;
ListNode *pre = dummy, *cur = head, *next = head->next;
while(cur && next){
ListNode *temp = next->next;
pre->next = next;
cur->next = next->next;
next->next = cur;
pre = cur;
cur = temp;
next = temp==NULL ? NULL : temp->next;
}
return dummy->next;
}

Leetcode 线性表 Swap Nodes in Pairs的更多相关文章

  1. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

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

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

  3. 【LeetCode】24. Swap Nodes in Pairs (3 solutions)

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

  4. LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation

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

  5. 【一天一道LeetCode】#24. Swap Nodes in Pairs

    一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...

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

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

  7. leetcode 题解 || Swap Nodes in Pairs 问题

    problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...

  8. 【LeetCode】24. Swap Nodes in Pairs

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

  9. LeetCode OJ 24. Swap Nodes in Pairs

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

随机推荐

  1. Reverse Integer - Palindrome Number - 简单模拟

    第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...

  2. Quiz 6b Question 8————An Introduction to Interactive Programming in Python

     Question 8 We can use loops to simulate natural processes over time. Write a program that calcula ...

  3. 在SAE上搭建自定义版本WordPress, 并用SAE Storage代替WordPress Uploads

    问题由来: 1. SAE中默认的WP for SAE版本太低, 导致某些基于新版本WordPress的插件不能使用. 2. SAE中单个APP的代码空间仅有100M. 将WordPress中uploa ...

  4. C语言之固定格式输出当前时间

    固定格式输出当前时间 编程实现将当前时间用以下形式输出:星期  月  日   小时:分:秒   年 代码如下: #include<stdio.h>#include<stdlib.h& ...

  5. spring mvc项目【转载】

    用了好几年的ssh2.最近打算研究下spring的mvc,看看如何,可以的话后期的项目将都是用springmvc+spring jdbc的形式,这样就少了其他框架的继承.由于以前没用过springmv ...

  6. 转: requirejs中文api (详细)

    RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可以用它来加速.优化代码,但其主要目的还是为了代码的模块化.它鼓励在使用脚本时以modul ...

  7. BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    题目 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 53  Solve ...

  8. 九度 和为S的连续正数序列

    题目1354:和为S的连续正数序列 时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:2008 解决:622 题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上 ...

  9. C# Setup package Uninstaller

    安裝的部分就不介紹了,網上一搜一大堆,這裡只介紹下卸載的部分. 1.在C:\Windows\System32 目录下找到 msiexec.exe 拷贝到相应的地方,可修改名称为 Uninstall.e ...

  10. switch语句:适用于一个条件有多个分支的情况---分支语句

    例1: 客服选择功能,然后按按键 Console.WriteLine("查花费请按1,查余额请按2,查流量请按3,办理业务请按4,宽带请按5,人工服务请按6,集团业务请按7"); ...