链表_模板

运行结果:

链表_模板_第1张图片

 

代码:

package Mo_Ban;

import java.util.Scanner;

//链表模板
public class Lian_Biao {
    /**
     * 值域
     */
    public static class ListNode {
        int val;
        ListNode next;

        public ListNode(int x) {
            val = x;
            next = null;
        }
    }

    /**
     * 主方法
     *
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ListNode l1 = new ListNode(-1);
        ListNode l2 = new ListNode(-1);
        System.out.print("请输入l1链表节点个数: ");
        int m = sc.nextInt();
        System.out.print("输入l1节点值: ");
        l1 = Create(m);
        System.out.print("结果: ");
        Print(l1);
        System.out.println();
        System.out.print("请输入l2链表节点个数: ");
        int n = sc.nextInt();
        System.out.print("输入l2节点值: ");
        l2 = Create(n);
        System.out.print("结果: ");
        Print(l2);
        System.out.println();
        System.out.println("删除l1链表指定元素: ");
        System.out.print("请输入要删除的元素: ");
        int a = sc.nextInt();
        l1 = Delete(l1, a);
        System.out.print("结果: ");
        Print(l1);
        System.out.println();
        System.out.println("在l1链表插入节点: ");
        ListNode b = new ListNode(-1);
        System.out.print("请输入要插入的节点值: ");
        b.val = sc.nextInt();
        l1 = Insert(l1, b);
        System.out.print("结果: ");
        Print(l1);
        System.out.println();
        System.out.println("合并链表l1、l2: ");
        ListNode l3 = new ListNode(-1);
        l3 = Merge(l1, l2);
        System.out.print("结果: ");
        Print(l3);

    }

    /**
     * 创建链表函数
     *
     * @param n
     * @return
     */
    public static ListNode Create(int n) {
        Scanner sc = new Scanner(System.in);
        //定义投节点
        ListNode head = new ListNode(-1);
        ListNode p = head, q = head;
        //给头节点赋值
        p.val = sc.nextInt();
        n--;
        //依次赋值,形成链
        while (n > 0) {
            q = p;
            p = new ListNode(-1);
            p.val = sc.nextInt();
            q.next = p;
            n--;
        }
        return head;
    }

    /**
     * 删除链表指定元素函数
     *
     * @param head
     * @param num
     * @return
     */
    public static ListNode Delete(ListNode head, int num) {
        //如果要删除的数是头节点,则直接next
        if (head.val == num) {
            head = head.next;
            return head;
        }
        ListNode p = head, q = head;
        //遍历链表
        for (p = head; p != null; p = p.next) {
            //找到要删除的数,进行删除操作
            if (p.val == num) {
                q.next = p.next;
                break;
            }
            q = p;
        }
        return head;
    }

    /**
     * 插入节点函数
     *
     * @param head
     * @param num
     * @return
     */
    public static ListNode Insert(ListNode head, ListNode num) {
        ListNode p = head, q = head;
        //如果值比头节点值还小,则插在head之前
        if (head.val > num.val) {
            head = num;
            num.next = p;
            return head;
        }
        //遍历链表,找到第一个比插入节点值大的元素,进行插入操作
        for (p = head; p != null; p = p.next) {
            if (p.val > num.val) {
                q.next = num;
                num.next = p;
                break;
            }
            q = p;
        }
        return head;
    }

    /**
     * 合并链表函数
     *
     * @param l1
     * @param l2
     * @return
     */
    public static ListNode Merge(ListNode l1, ListNode l2) {
        //定义虚拟指针
        ListNode pre = new ListNode(-1);
        ListNode cur = pre;
        //遍历l1,l2
        while (l1 != null && l2 != null) {
            //如果l1当前节点比l2当前节点小,则入链
            if (l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                //如果l1当前节点比l2当前节点>,则入链
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        //l1还有节点则直接连接
        if (l1 != null) {
            cur.next = l1;
        }
        //l2还有节点则直接连接
        if (l2 != null) {
            cur.next = l2;
        }
        return pre.next;
    }

    /**
     * 输出链表元素函数
     *
     * @param head
     */
    public static void Print(ListNode head) {
        for (ListNode p = head; p != null; p = p.next) {
            System.out.print(p.val + " ");
        }
    }
}

你可能感兴趣的:(Coding模板,链表,数据结构,散列表)