Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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<arr.length;i++) {
Node s = new Node(arr[i]);
p.next = s;
p = s;
}
}
public void createList()
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Node p = head;
for (int i = 0;i<n;i++)
{
int temp = sc.nextInt();
Node s = new Node(temp);
p.next = s;
p = s;
}
}
public void printList()
{
Node p = head.next;
while(p!=null)
{
System.out.print(p.data+" ");
p = p.next;
}
System.out.println();
}
//判断是否为递增有序链表
public boolean isSurge(LinkList L)
{
if (L==null)
{
return false;
}
else if (L.head.next==null)
{
return true;
}
Node p = L.head.next;
while(p.next!=null)
{
if (p.next.data < p.data)
{
return false;
}
p = p.next;
}
return true;
}
//将两个递增的有序链表合并为一个递增的有序链表
public void mergeSurgeList(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;
while(p1!=null && p2!=null)
{
if (p1.data > 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();
}
}
以上是关于Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表的主要内容,如果未能解决你的问题,请参考以下文章