topic61

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: 3 Imported by: 0

README

旋转列表

1. 题目描述

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

2. 示例

示例1 1

输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]

示例2 2

输入:head = [0,1,2], k = 4
输出:[2,0,1]

提示:

  • 链表中节点的数目在范围 [0, 500] 内
  • -100 <= Node.val <= 100
  • $0 \le k \le 2 * 10^9$

3. 解题

尾插法 若直接将尾部节点前插,则每次需要寻找尾指针,因此转化为将头部节点尾插,同时向右移动k个位置改为向左移动length-k个位置,length为链表长度

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 BuildListWithHead

func BuildListWithHead(data []int) *ListNode

func BuildListWithNoHead

func BuildListWithNoHead(nums []int) *ListNode

func (*ListNode) Slice

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

func (*ListNode) String

func (l *ListNode) String() string

Jump to

Keyboard shortcuts

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