作法請看下方function mergeLinkedListByOrder
import static utitled.SingleLinkedList.mergeLinkedListByOrder;
import static utitled.SingleLinkedList.showAllNodes;
public class Test {
public static void main(String[] args) {
// create first SingleLinkedList
SingleLinkedList singleLinkedList1 = new SingleLinkedList();
singleLinkedList1.addByOrder(new NumberNode(23));
singleLinkedList1.addByOrder(new NumberNode(1));
singleLinkedList1.addByOrder(new NumberNode(5));
singleLinkedList1.addByOrder(new NumberNode(49));
// create second SingleLinkedList
SingleLinkedList singleLinkedList2 = new SingleLinkedList();
singleLinkedList2.addByOrder(new NumberNode(4));
singleLinkedList2.addByOrder(new NumberNode(3));
singleLinkedList2.addByOrder(new NumberNode(17));
singleLinkedList2.addByOrder(new NumberNode(89));
singleLinkedList2.addByOrder(new NumberNode(99));
singleLinkedList2.addByOrder(new NumberNode(111));
singleLinkedList2.addByOrder(new NumberNode(133));
singleLinkedList2.addByOrder(new NumberNode(125));
singleLinkedList2.addByOrder(new NumberNode(112));
singleLinkedList2.addByOrder(new NumberNode(114));
NumberNode returnHead = mergeLinkedListByOrder(singleLinkedList1.getHead(), singleLinkedList2.getHead());
showAllNodes(returnHead);
}
}
/**
* 新建類別(SingleLinkedList)模擬單向鏈接串列
*/
class SingleLinkedList {
// step1:new a head node, without data
private NumberNode head = new NumberNode(0);
public NumberNode getHead() {
return head;
}
public void addByOrder(NumberNode numberNode) {
NumberNode temp = head;
while (true) {
if (temp.getNextNode() == null
|| temp.getNextNode().getNumberNo()
> numberNode.getNumberNo()) {// headNo:1 numberNode:2 headNextNode:4
// exchange position between three nodes
numberNode.setNextNode(temp.getNextNode());
temp.setNextNode(numberNode);
break;
} else if (temp.getNextNode().getNumberNo() == numberNode.getNumberNo()) {
System.out.printf("此數字編號:%d已存在,無法新增此節點\n", numberNode.getNumberNo());
break;
}
temp = temp.getNextNode();
}
}
public static void showAllNodes(NumberNode head) {
// to check this LinkedList has node or not? but not to change head node
NumberNode temp = head.getNextNode();
while (true) {
if (temp == null) {
// already moved to the end of LinkedList then return function
return;
}
System.out.println(temp);
temp = temp.getNextNode();
}
}
/**
*
* @param head1
* @param head2
* @return head 最終合併至的頭節點
*/
public static NumberNode mergeLinkedListByOrder(NumberNode head1, NumberNode head2) {
if (head1.getNextNode() == null || head2.getNextNode() == null) {
// any LinkedList without nodes, it's none sense to merge
if (head1.getNextNode() != null) {
return head1;
} else if (head2.getNextNode() != null) {
return head2;
}
}
// merge
// keep headNode depends on smaller first node
NumberNode head = head1.getNextNode().getNumberNo() < head2.getNextNode().getNumberNo() ? head1 : head2;
NumberNode returnHead = head;
NumberNode cur1 = head1.getNextNode();
NumberNode cur2 = head2.getNextNode();
while (cur1 != null && cur2 != null) {
if (cur1.getNumberNo() < cur2.getNumberNo()) {
// take smaller node to head
head.setNextNode(cur1);
//move to next node
cur1 = cur1.getNextNode();
} else {
// take smaller node to head
head.setNextNode(cur2);
// move to next node
cur2 = cur2.getNextNode();
}
// move to next node
head = head.getNextNode();
}
NumberNode next = null;
if (cur1 != null) {
next = cur1.getNextNode();
head.setNextNode(cur1);
head.getNextNode().setNextNode(next);
} else if (cur2 != null) {
next = cur2.getNextNode();
head.setNextNode(cur2);
head.getNextNode().setNextNode(next);
}
return returnHead;
}
}
/**
* 新建類別(NumberNode)模擬LinkedList的節點
*/
class NumberNode {
private int numberNo;
private NumberNode nextNode;
// constructor
public NumberNode(int no) {
this.numberNo = no;
}
public int getNumberNo() {
return numberNo;
}
public void setNumberNo(int numberNo) {
this.numberNo = numberNo;
}
public NumberNode getNextNode() {
return nextNode;
}
public void setNextNode(NumberNode nextNode) {
this.nextNode = nextNode;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("NumberNode{");
sb.append("numberNo=").append(numberNo);
sb.append(", nextNode=").append(nextNode);
sb.append('}');
return sb.toString();
}
}
如有敘述錯誤,還請不吝嗇留言指教,thanks!