栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表

代码如下:

package sjjgniub;

import java.util.linkedList;
import java.util.Scanner;

@SuppressWarnings("all")
public class linkList {

    private class Node
    {
        int data;
        Node next;

        public Node()
        {

        }

        public Node(int data)
        {
            this.data = data;
            next = null;
        }
    }

    Node head = null;

    public linkList(){
        head = new Node();
        if (head==null)
        {
            System.out.println("Error");
            return;
        }
        head.next = null;
    }

    public void createList(int[] arr)
    {
        Node p = head;
        for (int i = 0;i p2.data)
            {
                pc.next = p2;
                p2 = p2.next;
                pc = pc.next;
            }
            else if (p1.data < p2.data)
            {
                pc.next = p1;
                p1 = p1.next;
                pc = pc.next;
            }
            else
            {
                pc.next = p1;
                p1 = p1.next;
                p2 = p2.next;
                pc = pc.next;
            }
        }
        if (p1==null && p2==null)
        {
            pc.next = null;
        }
        else if (p1==null)
        {
            pc.next = p2;

        }
        else if (p2==null)
        {
            pc.next = p1;
        }
        La = null;


    }


    //将两个非递减的有序链表合并为一个非递增的有序链表
    public void mergeNotSurgeList(linkList La)
    {
        if (La==null)
        {
            System.out.println("Error");
            return ;
        }
        if(!isSurge(La))
        {
            System.out.println("No surge");
            return ;
        }

        if (!isSurge(this))
        {
            System.out.println("No surge");
            return ;
        }

        Node pc = head;
        Node p1 = head.next;
        Node p2 = La.head.next;
        pc.next = null;
        while(p1!=null&& p2!=null)
        {
            if (p1.data >= p2.data)
            {
                Node tmp = p2.next;
                p2.next = pc.next;
                pc.next = p2;
                p2 = tmp;
            }
            else
            {
                Node tmp = p1.next;
                p1.next = pc.next;
                pc.next = p1;
                p1 = tmp;
            }
        }

	 while(p2!=null)
       {
           Node tmp = p2.next;
           p2.next = pc.next;
           pc.next = p2;
           p2 = tmp;
       }

       while(p1!=null)
       {
           Node tmp = p1.next;
           p1.next = pc.next;
           pc.next = p1;
           p1 = tmp;
       }

        La = null;

    }

}

测试类:

package sjjgniub;

public class TestList {
    public static void main(String[] args) {
        linkList linkList = new linkList();
        int[] arr1 = {1,56,234,423,1000};
        linkList.createList(arr1);
        linkList.printList();
        linkList linkList2 = new linkList();
        int[] arr2 = {213,423,1000};
        linkList2.createList(arr2);
        linkList2.printList();
        System.out.println();

      //  linkList.mergeSurgeList(linkList2);
        linkList.mergeNotSurgeList(linkList2);
        linkList.printList();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317961.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号