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;
};

ListNode *mergeTwoList(ListNode *l1,ListNode *l2)
{
	ListNode *sum;
	ListNode *cur;
	ListNode *put;
	sum=NULL;//will be returned
	if((l1==NULL)||(l2==NULL))
		return l1?l1:l2;
	while(l1&&l2)
	{
		if(l1->value<l2->value)
		{
			put=l1;
			l1=l1->next;
		}
		else
		{
			put=l2;
			l2=l2->next;
		}
		if(sum==NULL)
		{
			sum=put;
			cur=sum;
		}
		else
		{
			cur->next=put;
			cur=cur->next;
		}
	}
	if(l1)
		cur->next=l1;
	if(l2)
		cur->next=l2;
	return sum;
}

int main()
{
	int n,m;
	printf("Please input the first list number:");
	while(scanf("%d",&m)!=EOF)
	{
		ListNode *h1=(ListNode*)malloc(sizeof(ListNode));
		ListNode *p1=h1;
		p1->next=NULL;
		printf("Please input the first list:\n");
		int tmp,i;
		scanf("%d",&tmp);
		p1->value=tmp;
		for(i=0;i<m-1;i++)
		{
			scanf("%d",&tmp);
			ListNode *q1=(ListNode*)malloc(sizeof(ListNode));
			q1->value=tmp;
			p1->next=q1;
			p1=q1;
			p1->next=NULL;
		}
		printf("Please input the second list number:");
		scanf("%d",&n);
		printf("Please input the second list:\n");
		ListNode *h2=(ListNode*)malloc(sizeof(ListNode));
		ListNode *p2=h2;
		p2->next=NULL;
		scanf("%d",&tmp);
		p2->value=tmp;
		for(i=0;i<n-1;i++)
		{
			scanf("%d",&tmp);
			ListNode *q2=(ListNode*)malloc(sizeof(ListNode));
			q2->value=tmp;
			p2->next=q2;
			p2=q2;
			p2->next=NULL;
		}
		printf("After Merge:");
		ListNode *list=mergeTwoList(h1,h2);
		ListNode *p=list;
		while(p)
		{
			printf("%d ",p->value);
			p=p->next;
		}
		free(h1);
		free(h2);
		printf("\n");
		printf("Please input the first list number:");
	}
	return 0;
}
时间: 2024-08-29 19:26:53

leetcode第21题-Merge Two Sorted Lists的相关文章

【LeetCode算法-21】Merge Two Sorted Lists

LeetCode第21题 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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 翻译: 合并两个有序链表并返回

LeetCode【21】 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. AC代码如下: ListNode* merge(ListNode* l1,ListNode* l2,int f1,int f2) { if(f1<f2) return merge(l2,l1,f2,f1); //f1

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长征系列】Merge k Sorted Lists

原题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:两条两条地合并.时间复杂度为O(n),n为所有链表节点和. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

LeetCode之“链表”:Merge Two Sorted Lists &amp;&amp; Merge k Sorted Lists

1. 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. 这道题目题意是要将两个有序的链表合并为一个有序链表.为了编程方便,在程序中引入dummy节点.具体程序如下: 1 /** 2 * Defin

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

(LeetCode 21)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. 题目要求: 合并两个有序链表 注意: 不能开辟新的结点空间 解题思路: 1.归并排序,创建一个新的头结点,从头到尾分别遍历两个链表,并依次比较其大小关系,每次将头指针指向小的那个. 2.递归思想 代码: /** *

链表经典题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(21) - Merge Two Sorted Lists

题目要求是,给你两个sorted LinkedList,然后把它们两个合并成一个新的LinkedList.思路很简单,就是比较ListNode L1 和 L2,哪个小就把那个node放到新的list里,并且移动相应ListNode的指针(L1或L2).注意其中一个为null,另外一个不为null的时候别漏掉就好. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val;