leetcode_21题——Merge Two Sorted Lists(链表)

Merge Two Sorted Lists

Total Accepted: 61585 Total Submissions: 188253My Submissions

Question Solution

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Hide Tags

Linked List

Have you met this question in a real interview?

Yes

No

Discuss

#include<iostream>
using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
	if(l1==NULL)
		return l2;
	if(l2==NULL)
		return l1;
	ListNode *ptr1=l1;//l1,l2作为剩下的未放进新链表的各自链表哦当前头结点指针
	ListNode *ptr2=l2;//
	ListNode *temp;//新链表的尾结点指针
	ListNode *root;//最终的头结点

	if(l1->val<l2->val)
	{
		temp=l1;
		root=l1;
		ptr1=l1->next;
		ptr2=l2;
	}
	else
	{
		temp=l2;
		root=l2;
		ptr1=l1;
		ptr2=l2->next;
	}

	while(1)
	{
		if(ptr2==NULL&&ptr1==NULL)
			break;
		if(ptr1==NULL&&ptr2!=NULL)
		{
			temp->next=ptr2;
			break;
		}
		if(ptr1!=NULL&&ptr2==NULL)
		{
			temp->next=ptr1;
			break;
		}

		if(ptr1->val<ptr2->val)
		{
			temp->next=ptr1;
			ptr1=ptr1->next;
			temp=temp->next;
		}
		else
		{
			temp->next=ptr2;
			ptr2=ptr2->next;
			temp=temp->next;
		}
	}
	return root;
}
int main()
{
	ListNode *root1=new ListNode(2);
	ListNode *root2=new ListNode(1);
	ListNode *ptr=mergeTwoLists(root1,root2);

	cout<<ptr->val<<‘ ‘<<ptr->next->val<<endl;
}

  

时间: 2024-10-06 04:15:33

leetcode_21题——Merge Two Sorted Lists(链表)的相关文章

链表经典题Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Hide Tags Linked List /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *ne

leetcode第22题--Merge k Sorted Lists

problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 先合并两个list,再根据归并排序的方法递归合并.假设总共有k个list,每个list的最大长度是n,那么运行时间满足递推式T(k) = 2T(k/2)+O(n*k).根据主定理,可以算出算法的总复杂度是O(nklogk).空间复杂度的话是递归栈的大小O(logk). /** * De

leetcode第21题-Merge Two Sorted Lists

本题目是意思是把两个有序的链表合成一个有序的链表,考察了归并算法和链表的操作. 代码也相对比较简单,简单说一下归并函数里三个指针的作用,sum是返回的第一个指针,cur是所要返回的链表里走到的位置,put是对于取到的l1或l2里的某一个指针节点.全部的可运行代码如下: #include<stdio.h> #include<string.h> #include<stdlib.h> struct ListNode{ int value; ListNode *next; };

leetcode 刷题之路 93 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 将k个有序链表合并成一个有序链表. 思路,之前做过两个有序链表的合并操作,对于k个链表,我们最先想到的就是能不能转化为我们熟悉的两个链表的合并,可以我们先将k个链表分为两部分,先对这两部分完成链表的有序合并操作,再对合并得到的两个链表进行合并,这是一个递归性质的描述,采用递归很容易实现这个程序,和数组

[Leetcode] Merge k sorted lists 合并k个已排序的链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:这题最容易想到的是,(假设有k个链表)链表1.2合并,然后其结果12和3合并,以此类推,最后是123--k-1和k合并.至于两链表合并的过程见merge two sorted lists的分析.复杂度的分析见JustDoIT的博客.算法复杂度:假设每个链表的平均长度是n,则1.2合并,遍历2n个

leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解:但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码. 代码一(超时未过): /** * Definition for singly-link

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

[LintCode] Merge Two Sorted Lists 混合插入有序链表

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 Exampl

LeetCode:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two l