数据结构与算法-腾讯面试题单向链表的翻转


单链表的翻转-腾讯面试题

翻转图演示
在这里插入图片描述

思路

定义一个节点reverseHead = new HeroNode();
从头到尾遍历原来的链表,没遍历一个节点,就将其取出,并放在新的链表的最前端;
原来链表的head.next = reverseHead.next;
理解:
在这里插入图片描述

蓝色的线是之前的链表节点之间的联系
橘色的线是我要改变成的节点之间的联系

编写方法reverseList()

   //Tencent Interview Questions
   public static void reverseList(HeroNode head){
       //if LinkedList is null or just one node return
       if( head.next == null || head.next.next == null){
           return;
       }
       HeroNode cur = head.next;
       HeroNode next = null;
       HeroNode reverseHead = new HeroNode(0,"","");

       //遍历原来的列表
       while(cur != null){
           next = cur.next;
           cur.next = reverseHead.next;
           reverseHead.next = cur;
           cur = next;

       }
       head.next = reverseHead.next;
   }

main方法加入

System.out.println("翻转后的链表");
HeroNode head = singleLinkedList.getHead();
reverseList(head);
singleLinkedList.list();

运行结果 在这里插入图片描述
至此实现了单向链表的翻转

全部代码

package com.chad.lambda;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        //进行测试
        HeroNode hero1 = new HeroNode(1,"宋江","及时雨");
        HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero3 = new HeroNode(3,"吴用","智多星");
        HeroNode hero4 = new HeroNode(4,"林冲","豹子头");

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //加入英雄
        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero2);
        singleLinkedList.addByOrder(hero3);


        //显示一把
        singleLinkedList.list();

        System.out.println("翻转后的链表");
        HeroNode head = singleLinkedList.getHead();
        reverseList(head);
        singleLinkedList.list();


    }

    //Tencent Interview Questions
    public static void reverseList(HeroNode head){
        //if LinkedList is null or just one node return
        if( head.next == null || head.next.next == null){
            return;
        }
        HeroNode cur = head.next;
        HeroNode next = null;
        HeroNode reverseHead = new HeroNode(0,"","");

        //遍历原来的列表
        while(cur != null){
            next = cur.next;
            cur.next = reverseHead.next;
            reverseHead.next = cur;
            cur = next;

        }
        head.next = reverseHead.next;


    }

    //Sina Interview Questions
    public static HeroNode findLastNode(HeroNode head, int k){
        if(  head.next == null){
            System.out.println("linkedList is Empty");
            return null;
        }
        int length = getLength(head);
        if(k<=0 || k> length){
            System.out.println("The input number is not right");
            return null;
        }
        //Define an auxiliary variable
        HeroNode cur = head.next;
        for (int i = 0; i < length - k; i++) {
            cur = cur.next;
        }
        return cur;
    }

    //get singleLinkedList's node number(not head node)
    public static int getLength(HeroNode head){
        if (head.next == null){
            System.out.println("The LinkList is Empty");
            return 0;
        }
        int length = 0;
        //定义一个辅助变量
        HeroNode cur = head.next;
        while(cur != null){
            length++;
            cur = cur.next;
        }
        return length;
    }

}

//定义SingleLinkedList管理我们的英雄
class SingleLinkedList{
    //初始化一个头节点,不存放具体数据
    private HeroNode head = new HeroNode(0,"","");

    public HeroNode getHead() {
        return head;
    }



    //不考虑顺序,找到链表的最后节点,将最后的next指向新的节点
    public void addHero(HeroNode heroNode){
        HeroNode temp = head;
        //遍历链表找到最后一个
        while(true){
            if(temp.next == null){
                break;
            }
            temp = temp.next;
        }

        temp.next = heroNode;
    }

    //添加方法二
    public void addByOrder(HeroNode heroNode){
        //因为头节点不能动
        HeroNode temp = head;
        //因为是处于添加到的节点上
        boolean flag = false;//添加的编号是否存在
        while(true){
            if(temp.next == null){
                break;
            }
            if(temp.next.no > heroNode.no){  //说明我的heroNode应该插入到这个temp的后面
                break;
            }else if (temp.next.no == heroNode.no){
                flag = true;
                break;
            }
            temp = temp.next; //后移
        }
        //判断flag的值
        if (flag){
            System.out.printf("准备插入的英雄%d已经有了\n",heroNode.no);
        }else {
            //插入数据
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }


    //显示链表
    public void list(){
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }

        HeroNode tmp = head.next;
        while (true){
            if(tmp.next == null){
                System.out.println(tmp);
                break;
            }
            System.out.println(tmp);
            tmp = tmp.next;
        }
    }
}

class HeroNode{
    public int no;
    public String name;
    public String nickName;
    public HeroNode next; //指向下一个节点
    //构造器
    public HeroNode(int hNo,String hName,String hNickName) {
        this.no = hNo;
        this.name = hName;
        this.nickName = hNickName;
    }
    //重写toString
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chad__chang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值