1. Swap Nodes in Pairs

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.

思路:模拟即可,思路不清楚时最好再纸上画画,确定节点指针的改变规则和顺序。交换顺序是这样的,用 cur 指针指向第一个,如果它不为空并且他的next也不为空则可以进行交换。交换时的步骤:(1)用temp暂存cur所指的第一个节点,(2)将cur指向第二个节点,也就是cur.next,(3)将第一个节点的next指向第二个节点的next,(4)第二个节点指向第一个节点,(5)将cur指向第三个节点,(6)这步容易忽略,要将改变后的第二个节点的next指向第四个节点(如果不为空的情况) 。这是因为一开始改变的时候,第一个节点前面没有节点,但是之后每个节点前面都是有节点的,所以之后不光要串连后面,还要串连前面。

public class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode cur = head;
ListNode newHead = head.next;
while (cur != null && cur.next != null) {
ListNode tmp = cur;
cur = cur.next;
tmp.next = cur.next;
cur.next = tmp;
cur = tmp.next;
      // 第6步
if (cur != null && cur.next != null) tmp.next = cur.next;
}
return newHead;
}
}

2. Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.

思路:可以使用加法来实现除法,但这题要注意的是一些临界情况,比如正负,溢出,除数被为0等情况。试了下结果Time Limit exceeded,而且这里发现了一个很有意思的问题,java中Math.abs(-2147483648)的返回值应该是什么?。那么改进下这个想法,不是一个一个加除数,而是成倍的增加除数。比如对于10/1,原来的算法是 1+1+1+1+1+1+1+1+1+1 总共循环10次,

LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation的更多相关文章

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

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

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

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

  3. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

  4. 【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 ...

  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. 洛谷 P4555 [国家集训队]最长双回文串 解题报告

    P4555 [国家集训队]最长双回文串 题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为\(n\)的串 ...

  2. Spring.NET中事务管理【转】

    http://www.cnblogs.com/GoodHelper/archive/2009/11/16/springnet_transaction.html 浏览了下写的比较清楚. 在.NET FC ...

  3. sass的mixin,extend,placeholder,function

    1. mixin 就是定义了一个函数,可以传参,并且设定默认值,css选择器可以通过@include来引用,mixin混入的代码,就是原样复制,不会合并,会造成冗余 例如: @mixin br6($b ...

  4. pg_basebackup: invalid tar block header size

    问题: 在使用pg_basebackup搭建备节点时,由于pg_basebackup本身使用的是int整型来保存传输的数据大小,当传输的数据大于4G的话,整数就会溢出,进而报出:pg_baseback ...

  5. 007.C++构造函数

    1.一个引例 //class head class complex //class body {} { public: complex(double r=0, double i) :re(r), im ...

  6. sub-G 无线芯片基础知识

    1.典型无线收发机编码 2.前导码的作用是使接收机的时钟和发射机同步(有待验证),如果接收机工作在WOR模式,前导码还有唤醒接收机的功能(接收一定数量的前导码),此时发射机必须发送较长的前导码才能把接 ...

  7. phantomjs 无法打开https网站

    最近一直在用phantomjs 自动登陆并爬取一些数据,突然发现爬取https类型的网站的时候无法正常操作了 困扰了两天的问题在经过google和stackoverflow的一番搜索后发现原来Phan ...

  8. JavaScript中检测数组的几种方式

    检测一个对象是否为数组的方式有: Array.isArray()          // true或false(es5) toString.call([]);       // [object Arr ...

  9. Android实现自动定位城市并获取天气信息

    定位实现代码: <span style="font-size:14px;">import java.io.IOException; import java.util.L ...

  10. 【BZOJ3339&&3585】mex [莫队][分块]

    mex Time Limit: 20 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 有一个长度为n的数组{a1,a2,. ...