leecode刷题(22)-- 反转数组

反转数组

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

进阶:

你可以迭代或递归地反转链表。你能否用两种方法解决这道题?


思路:

很明显,链表问题我们要通过改变指针指向来解决问题:

首先定义 per, key, last 三个指针:

pre    key  last
null 1 -> 2 -> 3 -> 4 -> null

key.next = pre :

pre    key  last
null <- 1 2 -> 3 -> 4 -> null

再让 pre = key, key = last, last = last.next

        pre  key  last
null <- 1 2 -> 3 -> 4 -> null

重复上述操作直到 last = null

                    pre key  last
null <- 1 <- 2 <- 3 <-4 5 -> null

最后让 key.next = pre 即可:

                     pre  key  last
null <- 1 <- 2 <- 3 <-4 <- 5 null

返回反转后的链表,其实也就是 key

代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode key = head;
ListNode last = head.next; while(last != null) {
key.next = pre;
pre = key;
key = last;
last = last.next;
}
key.next = pre;
return key;
}
}

优化:

上述代码在 leecode 能通过执行,但是有个问题,就是当 head = null 时,我们设置 ListNode last = head.next 就会出错,所以先判断 head(即我们设置的 key)是否为0,再设置 last 指针,然后改变设置的指针指向,当 key = null 时,我们返回 pre 。改为下述代码。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode key = head; while(key != null) {
ListNode last = key.next;
key.next = pre;
pre = key;
key = last;
}
return pre;
}
}

PS:

Java 中没有指针的概念,所以把我文章中所说的指针在代码中当作节点就好啦。

leecode刷题(22)-- 反转数组的更多相关文章

  1. leecode刷题(12)-- 整数反转

    leecode刷题(12)-- 整数反转 整数反转 描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: - ...

  2. leecode刷题(11)-- 反转字符串

    leecode刷题(11)-- 反转字符串 反转字符串 描述: 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh& ...

  3. leecode刷题(6)-- 两个数组的交集II

    leecode刷题(6)-- 两个数组的交集II 两个数组的交集II 描述: 给定两个数组,编写一个函数来计算它们的交集. 示例: 输入: nums1 = [1,2,2,1], nums2 = [2, ...

  4. leecode刷题(4)-- 存在重复数组

    leecode刷题(4)-- 存在重复数组 存在重复数组 题目描述: 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 ...

  5. leecode刷题(3)-- 旋转数组

    leecode刷题(3)-- 旋转数组 旋转数组 给定一个数组,将数组中的元素向右移动 K 个位置,其中 K 是非负数. 示例: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5, ...

  6. leecode刷题(16)-- 字符串转换整数

    leecode刷题(16)-- 字符串转换整数 字符串转换整数 描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格 ...

  7. leecode刷题(19)-- 最长公共前缀

    leecode刷题(19)-- 最长公共前缀 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: [&quo ...

  8. leecode刷题(10)-- 旋转图像

    leecode刷题(10)-- 旋转图像 旋转图像 描述: 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维 ...

  9. leecode刷题(8)-- 两数之和

    leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...

随机推荐

  1. js跳出for循环

    1 使用普通的for循环 注意foreach使用return或break都无法跳出循环 2 使用every跳出循环, every 方法会遍历数组中的所有元素来判断是否满足条件,如果有一个元素返回fal ...

  2. readonly const

    readonly:只读域,只能在初始化--声明初始化或构造器初始化--的过程中赋值,其他地方不能进行对只读域的赋值操作,否则编译器会报错.只读域可以是实例域也可以是静态域.只读域的类型可以是C#语言的 ...

  3. 01XC-1: 动态规划

  4. PHP里的进制

    1.进制转换函数: <?php function decto_bin($datalist,$bin) { static $arr=array(0,1,2,3,4,5,6,7,8,9,'A','B ...

  5. WSTMart开发文档

    WSTMart开发文档页面   PC版   开源版 授权版   序言   WSTMart安装协议   WSTMart电商系统安装   商城前台安装操作指南   用户中心指南   商家中心操作指南   ...

  6. CoreImage

    [CoreImage] CIContext is an object through which Core Image draws the results produced by a filter. ...

  7. mongo学习- 聚合project操作

    mongodb中聚合project操作,必须和其他的聚合一起使用,它的作用有以下几个: 1.返回我们想要显示的的字段 {"$project":{"name":1 ...

  8. scala高阶函数类型推断什么时候失效?

    class TypeInfer(self: Int, other: Int) { def test(num: Int, word: String, fun1: (Int, Int) => Int ...

  9. python sublime run快捷键设置

    一.Ctrl+Shift+P进行插件“sublimeREPL”安装 二.打开preferences->Key Binding-User,写入以下内容 [ { "keys": ...

  10. MFC中的一般经验之谈4

    MFC中的窗口控件,都是从CWnd类中继承的.MFC以及ResourceEditor支持的所有控件称为标准控件. 在对话框资源文件夹下,然后邮件新建添加新对话框,新建对话框后要在资源视图中的对话框文件 ...