先分析实现思路,具体实现代码在文章后面:
1.思路分析:
(1)先定义两个带有头的链表和一个新的链表头,如下图所示:
(2)接下来我们进行链表一和链表二的比大小即可。
首先,那一个链表节点的值小,先连接那一个。
其次,遇到链表的节点值相同时,我们优先连接链表一,对于链表二中的相同的值节点,我们直接跳过,不进行连接;
(3)如果有一个链表已经为空了,我们直接将另一个链表的剩余节点,连接到以空的链表末尾。
2.代码实现
笔者的节点链接采用对象的方式,以下是所定义的节点类;
public class CombineLinkedListDemo {
public static void main(String[] args) {
SingleLinkedList linkedList1 = new SingleLinkedList();
linkedList1.addListById(new HeroNode(1, "11", "及时雨"));
linkedList1.addListById(new HeroNode(2, "22", "智多星"));
linkedList1.addListById(new HeroNode(3, "33", "豹子头"));
linkedList1.addListById(new HeroNode(5, "鲁55", "花和尚"));
linkedList1.addListById(new HeroNode(8, "88", "白老虎"));
SingleLinkedList linkedList2 = new SingleLinkedList();
linkedList2.addListById(new HeroNode(2, "22", "智多星"));
linkedList2.addListById(new HeroNode(3, "33", "豹子头"));
linkedList2.addListById(new HeroNode(4, "44", "行者"));
linkedList2.addListById(new HeroNode(5, "鲁55", "花和尚"));
linkedList2.addListById(new HeroNode(6, "卢66", "玉麒麟"));
linkedList2.addListById(new HeroNode(7, "77", "入云龙"));
System.out.println("----------linkedList1---------------");
linkedList1.showAll();
System.out.println("----------linkedList2---------------");
linkedList2.showAll();
System.out.println("----------combineList2---------------");
HeroNode newHead = combineList(linkedList1, linkedList2);
SingleLinkedList newLinkedList = new SingleLinkedList();
newLinkedList.showAll(newHead);
}
public static HeroNode combineList(SingleLinkedList list1, SingleLinkedList list2) {
HeroNode head1 = list1.getHeadNode();
HeroNode head2 = list2.getHeadNode();
HeroNode newHead = new HeroNode(0, "", "");
HeroNode newTemp = newHead;
HeroNode temp1 = head1.next;
HeroNode temp2 = head2.next;
while (temp1 != null && temp2 != null) {
if (temp1.id < temp2.id) {
newTemp.next = temp1;
temp1 = temp1.next;
} else if (temp1.id == temp2.id) {
newTemp.next = temp1;
temp1 = temp1.next;
temp2 = temp2.next;
} else {
newTemp.next = temp2;
temp2 = temp2.next;
}
newTemp = newTemp.next;
}
if (temp1 == null) {
newTemp.next = temp2;
} else if (temp2 == null) {
newTemp.next = temp1;
}
return newHead.next;
}
}
class SingleLinkedList {
private HeroNode headNode;
public SingleLinkedList() {
this.headNode = new HeroNode();
}
public HeroNode getHeadNode() {
return headNode;
}
public void addListById(HeroNode heroNode) {
HeroNode temp = headNode;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.id > heroNode.id) {
break;
} else if (temp.next.id == heroNode.id) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
System.out.println("好汉的" + heroNode.id + "相同了");
} else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
public void showAll() {
HeroNode temp = headNode;
while (temp.next != null) {
temp = temp.next;
System.out.println(temp);
}
}
public void showAll(HeroNode head) {
HeroNode temp = head;
while (temp != null) {
System.out.println(temp);
temp = temp.next;
}
}
}
class HeroNode {
public int id;
public String name;
public String nickName;
public HeroNode next;
public HeroNode() {
}
public HeroNode(int id, String name, String nickName) {
this.id = id;
this.name = name;
this.nickName = nickName;
this.next = null; // Initialize next to avoid NullPointerException
}
@Override
public String toString() {
return "HeroNode{" +
"id=" + id +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
3.输出结果:
欢迎在评论区留言,互相切磋。