1. 程式人生 > >兩個無序單鏈合併成一個有序單鏈表

兩個無序單鏈合併成一個有序單鏈表

解題思路

  1. 兩個無序連結串列先轉換成兩個有序單鏈表
  2. 兩個有序單鏈表合併成一個有序單鏈表

程式碼:

import java.util.*;

//連結串列
class Node {
    int val;
    Node next;
    public Node(int val) {
        this.val = val;
    }
}

//自定義比較器
class Comparators {

    //單例模式(手動尷尬)
    private static Comparator comparator = null;

    public static Comparator getComparator
() { if(comparator == null) { comparator = new Comparator() { public int compare(Object ob1, Object ob2) { if(ob1 instanceof Node) return compare((Node)ob1, (Node)ob2); return 1; } //自定義連結串列排序規則
public int compare(Node n1, Node n2) { return n1.val - n2.val; } }; } return comparator; } } public class Main { //將無序單鏈錶轉化為有序單鏈表 public static Node sortOfList(Node p) { //暫存連結串列結點 ArrayList<Node> arr = new
ArrayList<Node>(); Node temp = p; while(temp != null) { arr.add(temp); temp = temp.next; } Collections.sort(arr, Comparators.getComparator()); Node ans = arr.get(0); Node temp2 = ans; for(int i = 1; i < arr.size(); i++) { temp2.next = arr.get(i); temp2 = temp2.next; } temp2 = null; return ans; } //兩個無序單鏈表p1,p2合併成一個有序單鏈表 public static Node mergeList(Node p1, Node p2) { //得到有序單鏈表 Node p11 = sortOfList(p1); Node p22 = sortOfList(p2); //合併單鏈表 Node ans; Node temp1 = p11; Node temp2 = p22; if(temp1.val <= temp2.val) { ans = temp1; temp1 = temp1.next; } else { ans = temp2; temp2 = temp2.next; } Node temp = ans; while(temp1 != null && temp2 != null) { if(temp1.val <= temp2.val) { temp.next = temp1; temp1 = temp1.next; } else { temp.next = temp2; temp2 = temp2.next; } temp = temp.next; } while(temp1 != null) { temp.next = temp1; temp = temp.next; temp1 = temp1.next; } while(temp2 != null) { temp.next = temp2; temp = temp.next; temp2 = temp2.next; } temp = null; return ans; } //測試 public static void main(String[] args) { Node p1 = new Node(3); p1.next = new Node(5); p1.next.next = new Node(4); p1.next.next.next = new Node(6); p1.next.next.next.next = new Node(7); Node p2 = new Node(8); p2.next = new Node(9); p2.next.next = new Node(10); p2.next.next.next = new Node(2); p2.next.next.next.next = new Node(1); p2.next.next.next.next.next = new Node(11); Node ans = mergeList(p1, p2); Node temp = ans; while(temp != null) { System.out.print(temp.val+" "); temp = temp.next; } System.out.println(); } }