lc023

package
v0.0.0-...-b071cee Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: GPL-3.0 Imports: 2 Imported by: 0

README

23.合并K个升序链表

1. 题目描述

给你一个链表数组,每个链表都已经按升序排列。

请你将所有链表合并到一个升序链表中,返回合并后的链表。

示例 1:

输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
  1->4->5,
  1->3->4,
  2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6

示例 2:

输入:lists = []
输出:[]

示例 3:

输入:lists = [[]]
输出:[]

提示:

  • k == lists.length
  • 0 <= k <= 10^4
  • 0 <= lists[i].length <= 500
  • -10^4 <= lists[i][j] <= 10^4
  • lists[i]升序 排列
  • lists[i].length 的总和不超过 10^4

标签 链表 分治 堆(优先队列) 归并排序

2. 解题

1.采用顺序合并的方法,先创建一个空的结果链表,依次将该链表与下一个链表合并,将结果存入结果链表中。 2.采用归并排序的思想,每轮将两个链表进行合并,直至合并成一个结果链表。

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
}

func BuildCircleListWithNoHead

func BuildCircleListWithNoHead(list []int, cursor int) *ListNode

BuildCircleListWithNoHead build a circle list, which tail point to list[cursor]

func BuildListWithHead

func BuildListWithHead(data []int) *ListNode

func BuildListWithNoHead

func BuildListWithNoHead(nums []int) *ListNode

func (*ListNode) HasCycle

func (l *ListNode) HasCycle() (bool, *ListNode)

HasCycle check if linklist has cycle structure, if not, return false, nil, if yes, return true and the cycle start node

func (*ListNode) Index

func (l *ListNode) Index(target int) *ListNode

Index return the node which value equals target

func (*ListNode) Len

func (l *ListNode) Len() int

Len return list length, when list has cycle structure, return -1

func (*ListNode) Slice

func (l *ListNode) Slice() []int

Slice return slice which contains all node's values in order.

func (*ListNode) String

func (l *ListNode) String() string

String implements Stringer interface.

func (*ListNode) Tail

func (l *ListNode) Tail() *ListNode

Tail return last list node

Jump to

Keyboard shortcuts

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