->4->4,return 1->2->3->4->5->6.

思路:

(1)题意为将两个有序链表合成一个有序链表。

(2)首先,分别对链表头结点判空,如果都为空,返回null;若L1为空,L2不为空,返回L1;如果L1为空,L2不为空,返回L2。

(2)其次,设置节点p为结果链表头结点,设置标志节点q指向结果链表尾部节点。

(3)再次,循环对待合并链表中的节点遍历,如果节点L1和L2都不为空,则比较其节点值,如果L1<L2,(第一次需要初始化p和q的值,p=L1,

q=p),将标志节点指向节点L1,标志节点后移,L1指向其后续节点,L1>=L2情况类似,直到循环结束。

(4)最后,需要判断未进行比较的节点,将这些节点追加为q的后续节点,返回p即为结果。

(5)其比较过程简单如下所示:

例如: L1: 1->3->5->13  L2: 2->4->14->17

(A)L1=1,L2=2,L1<L2,初始p=L1=1,q=p=1,L1=L1.next=3;

(B)L1=3,L2=2,L1>L2,此时,q.next=L2=2,q=q.next=2,L2=L2.next=4;

(C)L1=3,L2=4,L1<L2,此时,q.next=L1=3,q=q.next=3,L1=L1.next=5;

(D)L1=5,L2=4,L1>L2,此时,q.next=L2=4,q=q.next=4,L2=L2.next=14;

(E)L1=5,L2=14,L1<L2,此时,q.next=L1=5,q=q.next=5,L1=L1.next=13;

(F)L1=13,L2=14,L1<L2,此时,q.next=L1=13,q=q.next=13,L1=L1.next=null;

(G)由于L1为空,此时需将L2中后续节点追加到q后面即可。

算法代码实现如下所示:

public ListNode mergeTwoLists(ListNode L1, ListNode L2) {
	if (L1 == null && L2 == null  return null;
	if (L1 == null && L2 != null) return L2;
	if (L1 != null && L2 == null) return L1;

	ListNode p = null;
	ListNode q = p;
	while (L1 != null && L2 != null) {
		if (L1.val < L2.val) {
			if (p == null) {
				p = L1;
				q = p;
				L1 = L1.next;
				continue;
			}
			q.next = L1;
			q = q.next;
			L1 = L1.next;
		} else {
			if (p == null) {
				p = L2;
				q = p;
				L2 = L2.next;
				continue;
			}
			q.next = L2;
			q = q.next;
			L2 = L2.next;
		}
	}

	while (L1 != null) {
		q.next = L1;
		q = q.next;
		L1 = L1.next;
	}

	while (L2 != null) {
		q.next = L2;
		q = q.next;
		L2 = L2.next;
	}

	return p;
}

Leetcode_21_Merge Two Sorted Lists的更多相关文章

  1. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  2. [LeetCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

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

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

  4. No.023:Merge k Sorted Lists

    问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  5. Merge k Sorted Lists

    1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...

  6. 71. Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  7. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  8. Merge Two Sorted Lists

    Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...

  9. Java for LeetCode 023 Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

随机推荐

  1. print语句中逗号(,)和反斜杠(\)的区别

    逗号结尾:   禁止输出换行反斜杠结尾:强制输出换行 >>> print ('A','B') #用一个逗号结尾就可以禁止输出换行 A B >>> print ('A ...

  2. C++笔记003:从一个小程序开始

      原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 安装好VS2010后,从第一个小程序开始. 在学习C语言时,我首先输出了一个程序员非常熟悉的对这个世界的问候:hello world! ...

  3. python3+django2 开发易语言网络验证(上)

    创作背景: 在某论坛中下载到一套php开发易语言网络验证的教程,照着看下来,花了两天的时间,结果发现教程里开发的网络验证,以及随着教程一起给学员的源码,都存在着根本用不了的bug!我想要看看能不能在原 ...

  4. 百度地图JS 搜索悬浮窗功能

    这个需求的效果类似下面的截图,主要还是利用百度地图中自定义控件的功能,挺简单的.文档地址在这 http://lbsyun.baidu.com/index.php?title=jspopular 效果图 ...

  5. RDO Stack: No valid host was found. There are not enough hosts available.

    Issue: When you launch an instance in Newton, you may find that the instance cannot be started due t ...

  6. [图论]最大流问题(Maximum flow)的定义

    首先定义网络(network)N =(V,E), V表示顶点(Vertices)集合, E表示边(Edges)集合. s,t是V中的两个顶点,分别表示网络N中的源点(source)和汇点(sink). ...

  7. React Native之ScrollView控件详解

    概述 ScrollView在Android和ios原生开发中都比较常见,是一个 滚动视图控件.在RN开发中,系统也给我们提供了这么一个控件.不过在RN开发中 ,使用ScrollView必须有一个确定的 ...

  8. Django extra 和 annotate

    >>> qs=Question.objects.extra(select={'anum': 'SELECT COUNT(*) FROM questions_answer WHERE ...

  9. Python 表示无穷大的数

    我之前只知道设置初始值0.今天偶然在Python算法书上看到这个片段,从100个随机数里面找2个最靠近的自然数(不相等): from random import randrange seq = [ra ...

  10. 【安卓开发】用PageTransformer打造更好的动画效果

    Android的ViewPager类已经变成一个相当流行的Android应用组件了.它简单直观,并且提供了极好的功能.你可以经常在设置向导,图片画廊种看到它,它还是分开应用内容的良好方式. 标准的Vi ...