Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order.
Have you met this question in a real interview?
Yes
Example
Given 1->3->8->11->15->null, 2->null , return 1->2->3->8->11->15->null.
初始解题思路:
首先想到的是将第二条链表中的数据一个一个取出来插入排序进第一条链表中,可是具体实现发现太复杂了,手动写了很多无关的函数,考虑情况多,耗时多,不推荐。AC代码如下:
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2)
{
// write your code here
if(l1 == NULL) return l2;
ListNode * pl2 = l2;
while(pl2 != NULL)
{
if(l1->val > pl2->val)
l1 = ListInsertHead(l1,pl2->val);
else
ListInsert(l1,pl2->val);
pl2 = pl2->next;
}
return l1;
}
ListNode * ListInsertHead(ListNode * l1,int val)//将val值插入链表头
{
ListNode * newNode = new ListNode(val);
newNode->next = l1;
return newNode;
}
void ListInsert(ListNode * l1,int val)//将val值插入排序好的链表中依然有序
{
ListNode * newNode = new ListNode(val);
ListNode * pl1Pre = l1;
while(pl1Pre->next != NULL)
{
if(pl1Pre->next->val > val)
{
newNode->next = pl1Pre->next;
pl1Pre->next = newNode;
return;
}
pl1Pre = pl1Pre->next;
}
if(newNode->next == NULL)
pl1Pre->next = newNode;
}
};
推荐解题思路:
这就类似于归并排序中的归并操作。新建一条链表mergeList存储归并后的结果,再同时用l1与l2指针遍历两条链表,谁更小则加入mergeList尾部,如果一条链表遍历完,则直接把另外一条链表加入mergeList尾部。
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2)
{
// write your code here
ListNode * mergeList = new ListNode(0);
ListNode * temp = mergeList;
while(l1 || l2)
{
if(l1 == NULL)
{
temp->next = l2;
break;
}
else if( l2 == NULL)
{
temp->next = l1;
break;
}
else if(l1->val < l2->val)
{
temp->next = l1;
l1 = l1->next;
}
else
{
temp->next = l2;
l2 = l2->next;
}
temp = temp->next;
}
return mergeList->next;
}
};
JAVA代码:
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// write your code here
ListNode p1 = l1;
ListNode p2 = l2;
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
while(p1 != null || p2 != null){
if(p1 != null && p2 != null){
if(p1.val <= p2.val){
cur.next = p1;
p1 = p1.next;
}else{
cur.next = p2;
p2 = p2.next;
}
cur = cur.next;
}else if(p1 != null){
cur.next = p1;
break;
}else{
cur.next = p2;
break;
}
}
return dummy.next;
}
}
本文介绍了一种高效的方法来合并两个升序排列的链表,通过创建一个新的链表来存储合并后的结果,并提供了详细的C++及Java代码实现。
1473

被折叠的 条评论
为什么被折叠?



