Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表
生活随笔
收集整理的這篇文章主要介紹了
Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
代碼如下:
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();} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的世界如何做树屋
- 下一篇: java美元兑换,(Java实现) 美元