21、Merge Two Sorted Lists

 1     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
 2         ListNode* t;
 3         t=(ListNode*)malloc(sizeof(ListNode));
 4         ListNode *t1;
 5         t1=t;
 6         if(l1==NULL&&l2==NULL)
 7             return NULL;
 8         while(l1&&l2)
 9         {
10             if(l1->val<=l2->val)
11             {
12                 t1->next=l1;
13                 l1=l1->next;
14             }
15             else
16             {
17                 t1->next=l2;
18                 l2=l2->next;
19             }
20             t1=t1->next;
21         }
22         if(l1==NULL)
23         {
24             t1->next=l2;
25         }
26         if(l2==NULL)
27         {
28             t1->next=l1;
29         }
30         return t->next;

题目很简单,注意开辟新的链表指针时,需要用malloc分配空间,t=(ListNode*)malloc(sizeof(ListNode));

时间: 2024-11-09 15:47:27

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

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

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

C# 写 LeetCode easy #21 Merge Two Sorted Lists

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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 代

LeetCode 21 23:Merge Two Sorted Lists &amp; Merge K Sorted Lists

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. 题目分析:对两个有序列表进行合并,这个是数据结构基本题目比较简单,代码如下(这个代码有点长,可以简化): ListNode *mergeTwoL

leedCode练题——21. Merge Two Sorted Lists(照搬大神做法)

1.题目 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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4-&g

leetCode 21. Merge Two Sorted Lists 合并链表

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. 题目大意:合并两个有序的链表 思路:通过比较两个链表的节点大小,采用尾插法建立链表. 代码如下: /**  * Definition for singly-lin