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. Git----时光穿梭机之撤销修改05

    自然,你是不会犯错,不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行: $ cat readme.txtGit is a distributed version cont ...

  2. 将bmp文件转换为jpg文件

    procedure TForm1.Button1Click(Sender: TObject);(*压缩MBP为JPEG;但是没有提供压缩比可选项凑合用吧,大概1/3 ^_^:Note:必须加上JPEG ...

  3. 13.Roman to Integer (HashTable)

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  4. mysql 一次性插入上万条数据测试专用

    无聊期间 记录下 mysql 一次性插入上万条数据 测试的时候可以用 首先 创建一个表 t3 create table t3(id int)ENGINE = MyISAM; \d //    表示吧m ...

  5. log日志查看

    (1)类型1: TcLogCls::add_log($data, __METHOD__ . '::订单号:' . $order_no, 'tc_order'); (2)类型2:

  6. linux git server 简易搭建 (ssh访问)

    git的服务器搭建,如果无需权限控制,仅团队内部使用,初始化一个服务器仓库,其他人通过ssh访问这个文件夹即可.如需复杂的管理,建议使用gitlab. yum install git -y id gi ...

  7. sql语句语句中的正则查找

    举例: select tncl_id from tncl where tncl_id regexp'^0065'; 有一表,数据有10万多条,其中某列数据示例如下: 100000-200000-300 ...

  8. shell 脚本 查看班上同学的网络状态

    #!/bin/base # a=192.168.100. //定义变量 c=('王浩' '谢云生' '黄科杨' '何星宇' '张宸兵' '胡燕' '刘桃') //定义数组 for b in {101. ...

  9. 两种步骤 更改 EBS R12界面LOGO以及内容

    from:metalink more: Note 174219.1 - How To Change The Logo In The Oracle Application Menu Note 84975 ...

  10. 论DATASNAP远程方法支持自定义对象作参数

    论DATASNAP远程方法支持自定义对象作参数 DATASNAP远程方法已经可以支持自定义对象作参数,这是非常方便的功能. 1)自定义对象 type TMyInfo = class(TObject) ...