数据结构-单链表的增删改查操作+大厂面试题解题

package com.example.sgg.data.linkedlist;

import java.util.Stack;

/**
 * 单链表
 * Created by Administrator on 2021/6/28 0028.
 */
public class SingleLinkedList {

    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 linkedList = new SingleLinkedList();
        //加入
//        linkedList.add(hero1);
//        linkedList.add(hero2);
//        linkedList.add(hero3);
//        linkedList.add(hero4);
//        linkedList.list();

        //按顺序加入
        linkedList.addByOrder(hero1);
        linkedList.addByOrder(hero3);
        linkedList.addByOrder(hero2);
        linkedList.addByOrder(hero4);
        linkedList.list();

        //逆序打印
        reversePrint2(linkedList.head);
        linkedList.list();

//        //倒序
//        reverseLinkedList1(linkedList.head);
//        System.out.println("倒序1后的链表情况");
//        linkedList.list();
//        reverseLinkedList1(linkedList.head);
//        System.out.println("再倒序1后的链表情况");
//        linkedList.list();
//
//        //倒序
//        reverseLinkedList2(linkedList.head);
//        System.out.println("倒序2后的链表情况");
//        linkedList.list();
//        reverseLinkedList2(linkedList.head);
//        System.out.println("再倒序2后的链表情况");
//        linkedList.list();

        //修改节点
//        linkedList.update(new HeroNode(3, "哪吒", "飞天娃"));
//        System.out.println("修改后的链表情况");
//        linkedList.list();
//
//        //删除节点
//        linkedList.delete(2);
//        System.out.println("删除后的链表情况");
//        linkedList.list();
//
//        System.out.printf("有效节点个数%d", getLength(linkedList.getHead()));
//        System.out.println("倒数第3个节点是" + findLastIndexNode(linkedList.getHead(), 3));
    }

    //面试题1:查找有效节点个数
    public static int getLength(HeroNode head) {
        int length = 0;
        HeroNode temp = head.next;
        while (temp != null) {
            length++;
            temp = temp.next;
        }
        return length;
    }

    //面试题2:查找倒数第K个节点(新浪面试题)
    public static HeroNode findLastIndexNode(HeroNode head, int k) {
        if (head.next == null) {
            return null;
        }
        int total = getLength(head);
        if (k <= 0 || k > total) {
            return null;
        }
        HeroNode temp = head.next;
        for (int i = 0; i < total - k; i++) {
            temp = temp.next;
        }
        return temp;
    }

    //面试题3:单链表反转(腾讯面试题,有点难度)
    //解题思路1(因为只遍历了一次,因此优于解题思路2):
    //1、先定义一个节点reverseHead=new HeroNode()
    //2、遍历原来的链表,每遍历一个节点,将其取出,并放在新的链表的最前端(头插法)
    //3、将原来链表head.next=reverseHead.next
    public static void reverseLinkedList1(HeroNode head) {
        if (head.next == null || head.next.next == null) {
            //0个或一个节点无需反转
            return;
        }
        //定义一个辅助指针来遍历链表
        HeroNode cur = head.next;
        HeroNode next = null;//指向当前节点[cur]的下一个节点
        HeroNode reverseHead = new HeroNode(0, "", "");
        //遍历原来的链表,每遍历一个节点,将其取出,并放在新的链表的最前端(头插法)
        //动脑筋
        while (cur != null) {
            next = cur.next;//记录当前节点的后面一个节点,用于后移使用,防止中间操作出现节点变化

            //头插法(在链表最前端插入节点)
            cur.next = reverseHead.next;//将cur的指针指向新链表的最前端
            reverseHead.next = cur;//将新链表的头指针指向cur


            cur = next;//后移
        }
        //将原来链表head.next=reverseHead.next,实现单链表的反转
        head.next = reverseHead.next;
    }

    //面试题3:单链表反转(腾讯面试题,有点难度)
    //解题思路2:
    //1、遍历链表,将所有的节点按顺序放入一个数组中,数组大小为链表长度
    //2、先定义一个节点reverseHead=new HeroNode()
    //3、数组倒叙for循环,将节点添加到reverseHead后面,一个个添加
    //4、将原来链表head.next=reverseHead.next
    public static void reverseLinkedList2(HeroNode head) {
        if (head.next == null || head.next.next == null) {
            //0个或一个节点无需反转
            return;
        }
        //根据链表大小创建数组
        int length = getLength(head);
        HeroNode[] arr = new HeroNode[length];
        //将节点放入数组
        HeroNode temp = head;
        int i = 0;
        while (true) {
            if (temp.next == null) {
                break;
            }
            arr[i] = temp.next;
            i++;
            temp = temp.next;
        }
        //将数组倒序插入新的链表
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        for (i = length - 1; i >= 0; i--) {
            HeroNode heroNode = arr[i];
            heroNode.next = null;
            singleLinkedList.add(heroNode);
        }
        //将原链表的第一个节点指向新链表的第一个节点
        head.next = singleLinkedList.head.next;
    }

    //面试题4:逆序打印单链表
    //思路1:先将链表反转,再打印,但是会破坏原有链表结构,不建议
    //思路2:利用栈Stack的先进后出特性,来打印
    //TODO 思路3:还可以使用递归方式(已实现)
    public static void reversePrint(HeroNode head) {
        //使用思路2
        Stack<HeroNode> stack = new Stack<>();
        HeroNode temp = head;
        while (temp.next != null) {
            stack.add(temp.next);//入栈
            temp = temp.next;
        }
        while (!stack.empty()) {
            System.out.println(stack.pop());//出栈
        }
    }

    //思路3:使用递归
    public static HeroNode reversePrint2(HeroNode head) {
        if (head.next != null) {
            HeroNode heroNode = reversePrint2(head.next);
            System.out.println(heroNode.toString());
        }
        return head;
    }

    //TODO 课后练习:合并两个有序的单链表,合并之后链表依然有序,快慢指针?


    //先初始化一个头节点,头节点不要动,不存放具体的数据
    private HeroNode head = new HeroNode(0, "", "");

    public HeroNode getHead() {
        return head;
    }

    //添加节点到单向链表
    //思路:当不考虑编号顺序时
    //1、找到当前链表的最后一个节点
    //2、将最后一个节点的next指向新的节点
    public void add(HeroNode heroNode) {
        //因为head节点不能动,因此我们需要一个辅助变量temp
        HeroNode temp = head;
        //遍历链表找到最后一个节点
        while (true) {
            if (temp.next == null) {
                break;
            }
            //如果没有找到,就讲temp后移
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    //第二种方式,考虑按顺序添加节点,即根据排名将英雄插入指定位置
    //如果已存在该排名,则添加失败,并给出提示
    public void addByOrder(HeroNode heroNode) {
        //因为头结点不能动,因此找个辅助
        //因为单链表,因此我们找的temp是位于添加位置的前一个节点,否则插入不了
        HeroNode temp = head;
        boolean exist = false;//编号是否存在
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                break;
            }
            if (temp.next.no == heroNode.no) {
                exist = true;
                break;
            }
            temp = temp.next;
        }
        if (exist) {
            System.out.printf("英雄编号%d已存在\n", heroNode.no);
            return;
        }
        heroNode.next = temp.next;
        temp.next = heroNode;
    }


    //显示链表[遍历]
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
        }
        HeroNode temp = head;
        while (true) {
            temp = temp.next;
            System.out.println(temp.toString());
            if (temp.next == null) {
                break;
            }
        }
    }

    //根据编号来修改,编号不能修改
    public void update(HeroNode heroNode) {
        if (head.next == null) {
            System.out.printf("链表为空");
        }
        HeroNode temp = head.next;
        while (true) {
            if (temp == null) {//遍历完了还找不到对应的编号
                break;
            }
            if (temp.no == heroNode.no) {//找到对应编号的英雄节点
                break;
            }
            temp = temp.next;
        }
        if (temp == null) {
            System.out.printf("无对应编号%d的英雄,无法修改\n", heroNode.no);
            return;
        }
        temp.name = heroNode.name;
        temp.nickName = heroNode.nickName;
    }

    //删除节点
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("链表为空,无法删除");
        }
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                //找到该节点的前一个节点temp
                flag = true;
                break;
            }
            temp = temp.next;//移动到下一个节点
        }
        if (flag) {
            temp.next = temp.next.next;
        } else {
            System.out.printf("无对应编号%d的英雄,无法删除\n", no);
        }
    }

}

//定义一个HeroNode, 每个对象都是一个节点
class HeroNode {
    public int no;
    public String name;
    public String nickName;
    public HeroNode next;//指向下一个节点

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    //为了显示方便,重写toString
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值