problem0021

package
v0.0.0-...-db5e768 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2019 License: MIT Imports: 0 Imported by: 0

README

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. 先处理其中一条链为nil的情况,直接返回另一条链,这样可以简化后面的判断条件。
  2. 设置好链接头head和用于移动节点指针node
  3. 利用for循环反复比较,每次选取较小的节点,放在node.Next
  4. 处理l1或l2中剩余的节点

总结

合理地安排步骤,可以有效地减轻后面的判断条件和处理步骤,让整个函数更清晰易懂。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListNode

type ListNode struct {
	Val  int
	Next *ListNode
}

ListNode 是链接节点

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL