package com.zjl.dataStract;
public class MergeLinkList {
public static void main(String[] args) {
Node node1 = new Node(null, 44);
Node node2 = new Node(node1, 55);
Node node3 = new Node(node2, 66);
Node node4 = new Node(node3, 77);
Node node5 = new Node(node4, 88);
Node node0 = new Node(node5, 99);
Node node1B = new Node(null, 40);
Node node2B = new Node(node1B, 50);
Node node3B = new Node(node2B, 69);
Node node4B = new Node(node3B, 70);
Node node5B = new Node(node4B, 90);
Node node0B = new Node(node5B, 100);
Node node = mergeLinList(node0, node0B);
Node p = node;
while (p != null) {
System.out.print(p.data + " --> ");
p = p.next;
}
}
public static class Node {
public Node(Node next, Integer data) {
this.next = next;
this.data = data;
}
public Node next;
public Integer data;
}
public static Node mergeLinList(Node L1, Node L2) {
Node p = L1;
Node q = L2;
Node t = null;
if (p.data > q.data) {
t = p;
p = p.next;
} else {
t = q;
q = q.next;
}
Node r = t;
while (p != null && q != null) {
if (p.data > q.data) {
t.next = p;
p = p.next;
} else {
t.next = q;
q = q.next;
}
t = t.next;
}
if (p == null) {
t.next = q;
}
if (q == null) {
t.next = p;
}
return r;
}
}
|