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. HDOJ.1070 Milk(贪心)

    Milk 点我挑战题目 题意分析 每组测试数据给出一系列牛奶商品,分别是牛奶的品牌,价格,以及体积.在读取数据的时候,体积在200以下的牛奶直接忽略掉.并且每天要喝200ML的牛奶.但是无论牛奶体积有 ...

  2. 51nod 1257 背包问题 V3(分数规划)

    显然是分数规划...主要是不会求分数的形式,看了题解发现自己好傻逼QAQ 还是二分L值算出d[]降序选K个,顺便记录选择时候的p之和与w之和就可以输出分数形式了... #include<iost ...

  3. Django 2.0 URL

    Overview¶ A view is a “type” of Web page in your Django application that generally serves a specific ...

  4. Android HandlerThread分析

    基础概念:HandlerThread 是一个包含 Looper 的 Thread,我们可以直接使用这个 Looper 创建 Handler,本质上还是Thread Handler 必须要和 Loope ...

  5. Coconuts HDU - 5925 二维离散化 自闭了

    TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, ...

  6. Spark Streaming 的一些问题

    Spark Streaming 的一些问题,做选型前关注这些问题可以有效的降低使用风险. checkpoint checkpoint 是个很好的恢复机制.但是方案比较粗暴,直接通过序列化的机制写入到文 ...

  7. beego入门小坑

    刚接触beego,按照官网的文档操作,始终发现在orm操作数据的时候提示表不存在,数据库连接设置都没问题 "0 Error 1146: Table 'beego.archives' does ...

  8. C# 中的委托和事件(详解)

    C# 中的委托和事件 委托和事件在 .NET Framework 中的应用非常广泛,然而,较好地理解委托和事件对很多接触 C# 时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太 ...

  9. Epoll模型讲解

    1.流模型 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O操作的内核对象. 不管是文件,还是套接字,还是管道,我们都可以把他们看作流. 之后我们来讨论I/O的操作, ...

  10. git高级用法

    1.git未保存的代码怎么切换分支? 2.两个分支的代码怎么合并?怎么解决冲突? 常见报错: 1.Merge failed : Some unreacked working tree files wo ...