Leetcode43: Merge Two Sorted Lists

2024-08-28 19:58

本文主要是介绍Leetcode43: 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-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode* merge = new ListNode(0);ListNode* head = merge;while(l1&&l2){if(l1->val < l2->val){merge->next = l1;l1 = l1->next;merge = merge->next;}else{merge->next = l2;l2 = l2->next;merge = merge->next;}}if(l1){merge->next = l1;}if(l2){merge->next = l2;}return head->next;}
};


这篇关于Leetcode43: Merge Two Sorted Lists的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115742

相关文章

Python中的sort()和sorted()用法示例解析

《Python中的sort()和sorted()用法示例解析》本文给大家介绍Python中list.sort()和sorted()的使用区别,详细介绍其参数功能及Timsort排序算法特性,涵盖自适应... 目录一、list.sort()参数说明常用内置函数基本用法示例自定义函数示例lambda表达式示例o

pandas数据的合并concat()和merge()方式

《pandas数据的合并concat()和merge()方式》Pandas中concat沿轴合并数据框(行或列),merge基于键连接(内/外/左/右),concat用于纵向或横向拼接,merge用于... 目录concat() 轴向连接合并(1) join='outer',axis=0(2)join='o

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

56. Merge Interval

题目: 解答: 常规的合并,根据前后interval是否有交集判定。 代码: /*** Definition for an interval.* struct Interval {* int start;* int end;* Interval() : start(0), end(0) {}* Interval(int s, int e) : start

Java多线程编程模式实战指南:Two-phase Termination模式

文章来源: http://www.infoq.com/cn/articles/java-multithreaded-programming-mode-two-phase-termination?utm_source=infoq&utm_campaign=user_page&utm_medium=link 文章代码地址: https://github.com/Visce

多表连接的三种方式hash join,merge join,nested loop

多表之间的连接有三种方式:Nested Loops,Hash Join和 Sort Merge Join. 下面来介绍三种不同连接的不同:     一. NESTED LOOP: 对于被连接的数据子集较小的情况,嵌套循环连接是个较好的选择。在嵌套循环中,内表被外表驱动,外表返回的每一行都要在内表中检索找到与它匹配的行,因此整个查询返回的结果集不能太大(大于1 万不适合),要把返回

[LeetCode] 583. Delete Operation for Two Strings

题:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目 Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in

gitlab 上源码Merge后出现git gc的解决方案

问题: 远程主分支在merger其他分支的请求后,本地主分支pull 远程分支出现git gc * branch master -> FETCH_HEADAuto packing the repository for optimum performance. You may alsorun "git gc" manually. See "git help g

LeetCode - 33. Search in Rotated Sorted Array

33. Search in Rotated Sorted Array  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个数组,这个数组是由两个有序的数组拼接成的(无重复元素),在这个数组中查找元素k的下标. anal