单链表逆转

参考:https://zhuanlan.zhihu.com/p/488188589

单链表结构

package com.luck.living.util;

import lombok.Data;

@Data
public class Linked {
    public Integer value;
    public Linked next;
}

逆转算法:

package com.luck.living.util;

/**
 * 参考:https://zhuanlan.zhihu.com/p/488188589
 */
public class LinkedUtil {

    /**
     * 1.迭代反转法
     * @param linked
     * @return
     */
    public static Linked reversalIteration(Linked linked) {
        if (linked == null || linked.next == null) {
            return linked;
        }
        Linked beg = null;
        Linked mid = linked;
        Linked end = linked.next;
        while (true) {
            mid.next = beg;
            if (end == null) {
                break;
            }
            beg = mid;
            mid = end;
            end = end.next;
        }
        return mid;
    }

    /**
     * 2.递归反转法
     * @param linked
     * @return
     */
    public static Linked reversalRecursion(Linked linked) {
        if (linked == null || linked.next == null) {
            return linked;
        }
        Linked newHead = LinkedUtil.reversalRecursion(linked.next);
        linked.next.next = linked;
        linked.next = null;
        return newHead;

    }
    /**
     * 3.头插法
     * @param linked
     * @return
     */
    public static Linked reversalHead(Linked linked) {
        if (linked == null || linked.next == null) {
            return linked;
        }
        Linked newLinked = null;
        Linked temp = null;
        while (linked != null) {
            temp = linked;           //旧链表头指针指向的结点放进temp
            linked = linked.next;    //头指针指向下一个结点,相当于上个结点已经剔除于链表之外,
            temp.next = newLinked;  //被剔除的结点从新链表头部插入
            newLinked = temp;        //新链表的头指针指向新加入的结点
        }
        return newLinked;
    }
    /**
     * 4.逆置法
     * @param linked
     * @return
     */
    public static Linked reversalInversion(Linked linked) {
        if (linked == null || linked.next == null) {
            return linked;
        }
        Linked beg = linked;
        Linked end = linked.next;
        while (end != null) {
            beg.next = end.next;
            end.next = linked;
            linked = end;
            end = beg.next;
        }
        return linked;
    }

    /**
     * 头插法
     * @return
     */
    public static Linked initHead() {
        Linked current = null;
        for (int i = 6; i >= 0; i--) {
            Linked next = new Linked();
            next.value = i;
            next.next = current;
            current = next;
        }
        System.out.println("初始化数据:" + current);
        return current;
    }
    public static Linked init() {
        Linked linked = new Linked();
        Linked begin = linked;
        for (int i = 0; i < 7; i++) {
            Linked next = new Linked();
            next.value = i;
            next.next = null;
            begin.next = next;
            begin = next;
        }
        System.out.println("初始化数据:" + linked.next);
        return linked.next;
    }

    public static void main(String[] args) {
        System.out.println("迭代反转法:" + LinkedUtil.reversalIteration(initHead()));
        System.out.println("递归反转法:" + LinkedUtil.reversalRecursion(initHead()));
        System.out.println("头插反转法:" + LinkedUtil.reversalHead(initHead()));
        System.out.println("原地逆置反转法:" + LinkedUtil.reversalInversion(initHead()));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值