代码如下:
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();
}
}



