Leetcode 线性表 Swap Nodes in Pairs
本文为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的更多相关文章
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【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 ...
- 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 ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LeetCode 题解]:Swap Nodes in Pairs
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- leetcode 题解 || Swap Nodes in Pairs 问题
problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- 【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 ...
- 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 ...
随机推荐
- La=LaULb (循环链表)
#include<stdio.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void ...
- win64位 apache2.4 php5.4 mysql5.6
apache2.4 php5.4 mysql5.6 源文件下载 +以前的配置数据参考 链接:http://pan.baidu.com/s/1skfmGyT 密码:hqtp 比较好的参考资料 http: ...
- python 字典有序无序及查找效率,hash表
刚学python的时候认为字典是无序,通过多次插入,如di = {}, 多次di['testkey']='testvalue' 这样测试来证明无序的.后来接触到了字典查找效率这个东西,查了一下,原来字 ...
- 快捷查看dll的PublicKeyToken
@echo off d: cd D:\Win2003\Microsoft Visual Studio 10.0\VC\ call vcvarsall.bat x86 echo. if not '%1' ...
- android handler工作原理
android handler工作原理 作用 便于在子线程中更新主UI线程中的控件 这里涉及到了UI主线程和子线程 UI主线程 它很特别.通常我们会认为UI主线程将页面绘制完成,就结束了.但是它没有. ...
- 链接分析算法之:主题敏感PageRank
链接分析算法之:主题敏感PageRank 前面的讨论提到.PageRank忽略了主题相关性,导致结果的相关性和主题性降低,对于不同的用户,甚至有很大的差别.例如,当搜索“苹果”时,一个数码爱好 ...
- 建房子之前先挖地基 - Java BlockingQueue理解
最近一直在看<Think In Java>里关于并发部分的章节,读到第二十一章有一个有趣的比喻:必须先挖房子的地基,但是接下来可以并行的铺设钢结构和构建水泥部件,而这两项任务必须在混凝土浇 ...
- BZOJ 1677: [Usaco2005 Jan]Sumsets 求和
题目 1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 617 Solved: 344[Su ...
- javascript使用消息框
之前很多地方都用过alert,它的作用是弹出一个警告框,我们调用的方法是alert("输入的内容");其实更正确的写法是 window.alert("输入的内容" ...
- android 实现蓝牙自动配对连接
BluetoothConnectActivityReceiver.java:监听蓝牙配对的广播 代码: package com.imte.Broadcast; import com.imte.util ...