关于单向链表的总结__小七自学笔记

关于单向链表的总结

定义链表

class Node {
    int var;
    Node next;

    public Node() {
    }

    public Node(int var) {
        this.var = var;
    }
}

定义一个LinkdeList类,来对链表进行增、删、打印等操作

1.查找链表头节点和添加节点

class LinkdeList {
    public Node head = new Node(0); //头节点为空

    //得到头节点
    public Node getHead() {
        return head;
    }

    //末尾添加
    public void add(int var) {
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = new Node(var);
    }

    //指定位置添加
    public void orderadd(int var) {
        Node temp = head;
        Node cur = new Node(var);
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                flag = true;
                break;
            } else if (temp.next.var == var) {
                System.out.println("存在");
                break;
            } else if (temp.next.var > var) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            cur.next = temp.next;
            temp.next = cur;
        }

    }
}

2.删除节点

 //删除节点
    public void delNode(int var) {
        if (head.next == null) {
            return;
        }
        Node temp = head;
        Node cur = new Node(var);
        while (true) {
            if (temp.next == null) {
                System.out.println("不存在该节点");
            } else if (temp.next.var == var) {
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;
        }
    }

3.求链表总长

 //求链表总数目
    public int Length() {
        if (head.next == null) {
            return 0;
        }
        Node temp = head.next;
        int count = 0;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }

4.查找倒数第K个节点

//查找倒数第K个节点
    public int find(int index) {
        if (head.next == null) {
            return -1;
        }
        Node temp = head.next;
        int size = Length();
        int curindex = size - index;
        for (int i = 0; i < curindex; i++) {
            temp = temp.next;
        }
        return temp.var;
    }

5.打印链表

public void print() {
        if (head.next == null) {
            return;
        }
        Node temp = head.next;
        while (temp != null) {
            System.out.print(temp.var + " ");
            temp = temp.next;
        }
        System.out.println();
    }

创建包含main方法的linkedListTest类,对链表进行测试

public class linkedListTest {
    public static void main(String[] args) {
        LinkdeList list1 = new LinkdeList();
        LinkdeList list2 = new LinkdeList();
        list1.add(1);
        list1.add(2);
        list1.add(4);
        list1.orderadd(3);
        list1.orderadd(5);
        list1.orderadd(6);
        list2.add(7);
        list2.add(8);
        list2.add(9);
        list1.print();
        list2.print();
        Node head1= list1.getHead();
        Node head2= list2.getHead();
        int count = list1.Length();
        System.out.println("总长度为:" + count);
        int f1 = list1.find(2);
        System.out.println("寻找的倒数第2节点为:" + f1);
        list1.delNode(2);
        System.out.println("删除后的列表为:");
        list1.print();
    }
}

1.反转链表

//反转链表
    public static void reverselist(Node head) {
        if (head.next == null || head.next.next == null){
            return;
        }
        Node cur = head.next;
        Node reverse = new Node();
        Node next = null;
        while (cur != null) {
            next = cur.next;
            cur.next = reverse.next;//当前节点的下一节点,和新链表的下一个节点相连接
            reverse.next = cur;//新链表的下一个节点,指向当前节点
            cur = next;
        }
        head.next = reverse.next;
    }

2.逆序打印链表

//逆序打印列表
    public static void reversePrintList(Node head){
        Stack<Node> stack = new Stack<>();
        if (head.next == null){
            return;
        }
        Node temp = head.next;
        while(temp != null){
            stack.push(temp);
            temp = temp.next;
        }
        while (stack.size()>0){
            System.out.print(stack.pop().var + " ");
        }
    }

3.合并两个有序链表

 //合并两个有序的链表
    public static Node mergeList(Node head1,Node head2){
        if (head1 == null){
            return head2;
        }else if (head2 == null){
            return head1;
        } else if (head1 == null && head2 == null){
            return null;
        }
        Node temp = null;

        if (head1.var <= head2.var){
            temp = head1;
            temp.next = mergeList(head1.next,head2);
        }else if(head1.var > head2.var){
            temp = head2;
            temp.next = mergeList(head1.next,head2);
        }
        return temp;
    }

全部代码

package com.atguigu.NodeLikned;

import java.util.Stack;

public class linkedListTest {
    public static void main(String[] args) {
        LinkdeList list1 = new LinkdeList();
        LinkdeList list2 = new LinkdeList();
        list1.add(1);
        list1.add(2);
        list1.add(4);
        list1.orderadd(3);
        list1.orderadd(5);
        list1.orderadd(6);
        list2.add(7);
        list2.add(8);
        list2.add(9);
        list1.print();
        list2.print();
        Node head1= list1.getHead();
        Node head2= list2.getHead();
        /*int count = list1.Length();
        System.out.println("总长度为:" + count);
        int f1 = list1.find(2);
        System.out.println("寻找的倒数第2节点为:" + f1);
        list1.delNode(2);
        System.out.println("删除后的列表为:");

        //list.print();

        System.out.println(head1.var);
        reverselist(head1);
        list1.print();
        reversePrintList(head1);*/
        Node m1 = mergeList(head1.next,head2.next);
        while (m1!= null) {
            System.out.print(m1.var + " ");
            m1 = m1.next;
        }
    }

    //反转链表
    public static void reverselist(Node head) {
        if (head.next == null || head.next.next == null){
            return;
        }
        Node cur = head.next;
        Node reverse = new Node();
        Node next = null;
        while (cur != null) {
            next = cur.next;
            cur.next = reverse.next;//当前节点的下一节点,和新链表的下一个节点相连接
            reverse.next = cur;//新链表的下一个节点,指向当前节点
            cur = next;
        }
        head.next = reverse.next;
    }
    //逆序打印列表
    public static void reversePrintList(Node head){
        Stack<Node> stack = new Stack<>();
        if (head.next == null){
            return;
        }
        Node temp = head.next;
        while(temp != null){
            stack.push(temp);
            temp = temp.next;
        }
        while (stack.size()>0){
            System.out.print(stack.pop().var + " ");
        }
    }
    //合并两个有序的链表
    public static Node mergeList(Node head1,Node head2){
        if (head1 == null){
            return head2;
        }else if (head2 == null){
            return head1;
        } else if (head1 == null && head2 == null){
            return null;
        }
        Node temp = null;

        if (head1.var <= head2.var){
            temp = head1;
            temp.next = mergeList(head1.next,head2);
        }else if(head1.var > head2.var){
            temp = head2;
            temp.next = mergeList(head1.next,head2);
        }
        return temp;
    }
}

class LinkdeList {
    public Node head = new Node(0); //头节点为空

    //得到头节点
    public Node getHead() {
        return head;
    }

    //末尾添加
    public void add(int var) {
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = new Node(var);
    }

    //指定位置添加
    public void orderadd(int var) {
        Node temp = head;
        Node cur = new Node(var);
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                flag = true;
                break;
            } else if (temp.next.var == var) {
                System.out.println("存在");
                break;
            } else if (temp.next.var > var) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            cur.next = temp.next;
            temp.next = cur;
        }

    }

    //求链表总数目
    public int Length() {
        if (head.next == null) {
            return 0;
        }
        Node temp = head.next;
        int count = 0;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }

    //查找倒数第K个节点
    public int find(int index) {
        if (head.next == null) {
            return -1;
        }
        Node temp = head.next;
        int size = Length();
        int curindex = size - index;
        for (int i = 0; i < curindex; i++) {
            temp = temp.next;
        }
        return temp.var;
    }

    //删除节点
    public void delNode(int var) {
        if (head.next == null) {
            return;
        }
        Node temp = head;
        Node cur = new Node(var);
        while (true) {
            if (temp.next == null) {
                System.out.println("不存在该节点");
            } else if (temp.next.var == var) {
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;
        }
    }

    //打印节点
    public void print() {
        if (head.next == null) {
            return;
        }
        Node temp = head.next;
        while (temp != null) {
            System.out.print(temp.var + " ");
            temp = temp.next;
        }
        System.out.println();
    }
}

//链表

class Node {
    int var;
    Node next;

    public Node() {
    }

    public Node(int var) {
        this.var = var;
    }
}


    //打印节点
    public void print() {
        if (head.next == null) {
            return;
        }
        Node temp = head.next;
        while (temp != null) {
            System.out.print(temp.var + " ");
            temp = temp.next;
        }
        System.out.println();
    }
}

//链表

class Node {
    int var;
    Node next;

    public Node() {
    }

    public Node(int var) {
        this.var = var;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小七rrrrr

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

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

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

打赏作者

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

抵扣说明:

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

余额充值