算法38:反转链表【O(n)方案】

一、需求

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

在这里插入图片描述

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

在这里插入图片描述

输入:head = [1,2]
输出:[2,1]

示例3:

输入:head = []
输出:[]

提示:

  • 链表中节点的数目范围是 [0, 5000]
  • -5000 <= Node.val <= 5000

进阶:

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

二、思路分析图

(一)递归方案

在这里插入图片描述在这里插入图片描述

在这里插入图片描述

三、代码

(一)公共代码(链表类)

package com.bessky.pss.wzw.SuanFa;

import cn.hutool.core.util.StrUtil;

/**
 * 链表类
 *
 * @author 王子威
 * @date 2021/4/21
 */
public class ListNode
{
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    @Override
    public String toString()
    {
        ListNode ln = this;
        StringBuilder sb = new StringBuilder();

        while(ln != null){
            if (StrUtil.isEmpty(sb))
            {
                sb.append("[" + ln.val);
            }
            else
            {
                sb.append("," + ln.val);
            }
            ln = ln.next;
        }
        sb.append("]");
        return sb.toString();
    }
}

(二)数据初始化

/**
 * 入口
 * 206、反转链表
 * 输入:
 * head1 = [1,2,3,4,5]
 * head2 = [1,2,3,4,5]
 * 输出:
 * result1 = [5,4,3,2,1]
 * result2 = [5,4,3,2,1]
 * 解释:
 * 1.递归方案
 * 2.O(n)方案
 */
@Test
public void suanfa38()
{
    // 初始化
    ListNode head1 = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
    ListNode head2 = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));

    // 打印
    // 递归方案
    ListNode result1 = this.recursionReverseList(head1);
    System.out.println("result1 = " + result1.toString());

    // O(n)方案【迭代方案】
    ListNode result2 = this.forReverseList(head2);
    System.out.println("result2 = " + result2.toString());
}

(三)递归方案

/**
 * 递归方案
 *
 * @param head
 * @return
 */
private ListNode recursionReverseList(ListNode head)
{
    // 如果head为null 说明这个链表就没有数据
    // 如果下一个head为null,说明这个链表到最后一个值【节点】了【5到这里就直接返回了】
    if (head == null || head.next == null)
    {
        return head;
    }

    // 递归调用:不到最后一个节点,递归下一个head节点
    // head.next=2->head.next=3->head.next=4->head.next=5
    ListNode nextNode = this.recursionReverseList(head.next);

    // 5节点到不了,只有5节点以下的值才能来,因为5节点就是最后一个值
    // 5 -> 4 : 5节点指向4节点【4下一个节点5,5下一个节点指向4】
    // 4 -> 3 : 4节点指向3节点
    // 3 -> 2 : 3节点指向2节点
    // 2 -> 1 : 2节点指向1节点
    head.next.next = head;
    // 把4 -> 5 的指向删除【4的下一个节点】
    // 把3 -> 4 的指向删除
    // 把2 -> 3 的指向删除
    // 把1 -> 2 的指向删除
    head.next = null;

    // nextNode[5,4]->nextNode[5,4,3]->nextNode[5,4,3,2]->nextNode[5,4,3,2,1]->结束递归
    return nextNode;
}

(四) O(n)方案【迭代方案】

/**
 * O(n)方案【迭代方案】
 *
 * @param head
 * @return
 */
private ListNode forReverseList(ListNode head)
{
    ListNode node = null;
    for (ListNode temp = head;temp != null; temp = temp.next)
    {
        //node[1] -> node[2,1] -> node[3,2,1] -> node[4,3,2,1] -> node[5,4,3,2,1]
        node = new ListNode(temp.val, node);
    }
    return node;
}

(五)结果图

在这里插入图片描述

作者:王子威

四、总结

  • 学习了反转链表算法
  • 有点久没有些算法了,看的两眼冒金星,参考了网络解法,感觉O(n)方案很精妙
  • 算法兴趣+1 总:38
  • 加强了对算法的分析能力
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值