赞
踩
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
原题链接:https://leetcode.cn/problems/merge-two-sorted-lists/
需要注意,虽然样例中输入的两个列表是整数数组的形式,但其实官网的评测机内部将其分别先构建成以1为头节点的单链表(样例1为例),而传入mergeTwoLists方法的实参也正为每个链表的头节点。
这道题在数据结构链表内容中属于是比较经典的,解这道题涉及到的知识点主要是以下两点:
对于两个不带头节点的有序单链表,通过依次比较各节点的数据大小,拼接合并得到新的有序单链表。
以样例1为例,分析其合并过程:
大致流程就是从两个链表的头结点开始,依次比较对应节点数据的大小,根据比较结果拼接合并结果,具体地:
在实现时,需要在进入mergeTwoLists方法一开始就考虑以下三种特殊情况:
此外,因为采用的是不带头节点的单链表,所以在传入实参时直接传入对应链表的第一个节点即可。
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1==null && list2==null){ // l1=l2=[]
return null;
}
if(list1 == null){ // l1=[],l2=[x,x,x,x,x...]
return list2;
}
if(list2 == null){ // l1=[x,x,x,x,x...],l2=[]
return list1;
}
// 链表头结点不能动,否则后续节点无法查找,所以需要设置各自对应的辅助节点
ListNode tempNode1=list1; // 令辅助节点为链表l1的头结点
ListNode tempNode2=list2; // 令辅助节点为链表l2的头结点
ListNode resNode=new ListNode(-1); //创建合并结果链表的头结点
ListNode tempNode=resNode; // 令辅助节点为合并链表的头结点
// 循环比较节点大小,并插入结果链表res中
while (tempNode1!=null&&tempNode2!=null){
if(tempNode1.val<tempNode2.val){
tempNode.next=tempNode1;
tempNode1=tempNode1.next;
}else {
tempNode.next=tempNode2;
tempNode2=tempNode2.next;
}
tempNode=tempNode.next;
}
if(tempNode1==null){ // list1到达链尾,直接将list2的剩余节点插入到res中
tempNode.next=tempNode2;
}
if(tempNode2==null){ // list2到达链尾,直接将list1的剩余节点插入到res中
tempNode.next=tempNode1;
}
return resNode.next; // 返回头结点resNode的next域所指向的节点
}
package com.north.leetcode;
/**
* LeetCode21:合并两个有序链表
*/
import java.util.List;
public class mergeLinkList {
public static void main(String[] args) {
LinkedList list1=new LinkedList();
ListNode node3=new ListNode(4);
ListNode node2=new ListNode(2);
ListNode node1=new ListNode(1);
list1.insertByTail(node1);
list1.insertByTail(node2);
list1.insertByTail(node3);
list1.printList();
System.out.println("--------------------------------");
// list1.printByCustom(node2);
LinkedList list2=new LinkedList();
ListNode node23=new ListNode(4);
ListNode node22=new ListNode(3);
ListNode node21=new ListNode(1);
list2.insertByHead(node23);
list2.insertByHead(node22);
list2.insertByHead(node21);
list2.printList();
System.out.println("--------------------------------");
new LinkedList().mergeTwoLists(node1,node21).printByCustom();
}
}
class LinkedList{
ListNode head;
public LinkedList(){
}
// 尾插法
public void insertByTail(ListNode node){
if (head==null){
head=node;
}else {
ListNode tempNode = head;
while (tempNode.next != null) {
tempNode = tempNode.next;
}
tempNode.next = node;
}
}
// 头插法
public void insertByHead(ListNode node){
if (head != null) {
node.next = head;
}
head=node;
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1==null && list2==null){ // l1=l2=[]
return null;
}
if(list1 == null){ // l1=[],l2=[x,x,x,x,x...]
return list2;
}
if(list2 == null){ // l1=[x,x,x,x,x...],l2=[]
return list1;
}
// 链表头结点不能动,否则后续节点无法查找,所以需要设置各自对应的辅助节点
ListNode tempNode1=list1; // 令辅助节点为链表l1的头结点
ListNode tempNode2=list2; // 令辅助节点为链表l2的头结点
ListNode resNode=new ListNode(-1); //创建合并结果链表的头结点
ListNode tempNode=resNode; // 令辅助节点为合并链表的头结点
// 循环比较节点大小,并插入结果链表res中
while (tempNode1!=null&&tempNode2!=null){
if(tempNode1.val<tempNode2.val){
tempNode.next=tempNode1;
tempNode1=tempNode1.next;
}else {
tempNode.next=tempNode2;
tempNode2=tempNode2.next;
}
tempNode=tempNode.next;
}
if(tempNode1==null){ // list1到达链尾,直接将list2的剩余节点插入到res中
tempNode.next=tempNode2;
}
if(tempNode2==null){ // list2到达链尾,直接将list1的剩余节点插入到res中
tempNode.next=tempNode1;
}
return resNode.next; // 返回头结点resNode的next域所指向的节点
}
// 遍历整个链表
public void printList(){
ListNode tempNode=head;
while (tempNode!=null){
System.out.println(tempNode);
tempNode=tempNode.next;
}
}
// 从指定的节点开始遍历
// public void printByCustom(ListNode node){
// ListNode tempNode=node;
// while (tempNode!=null){
// System.out.println(tempNode);
// tempNode=tempNode.next;
// }
// }
}
// 定义链表节点
class ListNode{
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
@Override
public String toString() {
return "ListNode{" +
"val=" + val +
'}';
}
//从指定的节点开始遍历
public void printByCustom(){
ListNode tempNode=this;
while (tempNode!=null){
System.out.println(tempNode);
tempNode=tempNode.next;
}
}
}
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。