当前位置:网站首页>March 27, 2021: give you a head node of the linked list, and rotate the linked list
March 27, 2021: give you a head node of the linked list, and rotate the linked list
2022-06-24 17:17:00 【Fuda scaffold constructor's daily question】
2021-03-27: Give you a list of the head node head , Rotate the list , Move each node of the list to the right k A place . Input :head = 1→2→3→4→5, k = 2, Output :4→5→1→2→3.
Fuda answer 2020-03-27:
1. Find the tail node and calculate the number of linked list nodes .
2. If k Greater than the number of equilinked list nodes , Need modulus ,k It must be [0, Number of nodes ) Within limits . If k=0, Directly back to the head node .
3. Find the reciprocal k+1 The node of .
4. Cache penultimate k node ans.
5. Tail node to head node .
6. Reciprocal k+1 Node Next The pointer is null .
7. return ans.
The code to use golang To write , The code is as follows :
package main
import "fmt"
func main() {
head := &ListNode{Val: 1}
head.Next = &ListNode{Val: 2}
head.Next.Next = &ListNode{Val: 3}
head.Next.Next.Next = &ListNode{Val: 4}
printlnLinkNodeList(head)
k := 6
fmt.Println("k =", k)
fmt.Println("----------------")
ret := rotateRight(head, k)
printlnLinkNodeList(ret)
}
type ListNode struct {
Val int
Next *ListNode
}
func rotateRight(head *ListNode, k int) *ListNode {
if head == nil {
return head
}
// Find tail nodes and count
cnt := 1
tail := head
for tail.Next != nil {
cnt++
tail = tail.Next
}
k = k % cnt
if k == 0 { // It's just the head node , You don't have to operate .
return head
}
// Find the penultimate k+1 node
fast := head
slow := head
k++
for k > 0 {
fast = fast.Next
k--
}
for fast != nil {
fast = fast.Next
slow = slow.Next
}
// Cache results
ans := slow.Next
// Tail node to head node
tail.Next = head
// Reciprocal k+1 Node none Next The pointer
slow.Next = nil
return ans
}
// Linked list printing
func printlnLinkNodeList(head *ListNode) {
cur := head
for cur != nil {
fmt.Print(cur.Val, "\t")
cur = cur.Next
}
fmt.Println()
}The results are as follows :
边栏推荐
- Collect tke logs through daemonset CRD
- Sigai intelligent container damage identification products are deployed in Rizhao Port and Yingkou Port
- What securities dealers recommend? Is it safe to open an account online now?
- 让UPS“印象派用户”重新认识可靠性
- 实现TypeScript运行时类型检查
- Low education without food? As an old Android rookie in the past six years, I was the most difficult one
- Install Clickhouse client code 210 connection referred (localhost:9000)
- New MySQL 8.0 feature - enhanced logical backup recovery
- Introduction to koa (III) koa routing
- 跟着Vam一起学习Typescript(第一期)
猜你喜欢

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

Why do you develop middleware when you are young? "You can choose your own way"

MySQL learning -- table structure of SQL test questions
随机推荐
Following the previous SYSTEMd pit
Management system permission design
H265 video streaming web page without plug-in player easywasmlayer Troubleshooting and solution of JS unable to set cover photo
CentOS 7 installing SQL server2017 (Linux)
[web] what happens after entering the URL from the address bar?
[play with Tencent cloud] check 9 popular Tencent cloud products
"Gambler" bubble Matt turns around
[play Tencent cloud] experience and development of game multimedia engine (II)
AFG EDI requirements details
5g brings opportunities and challenges. Are you ready to defend against DDoS?
How to collect and define project requirements in the early stage of EDI project implementation?
集体突破之后,中国公有云的下一步落在哪里?
What securities dealers recommend? Is it safe to open an account online now?
Industrial security experts talk about how to guarantee the safety of data elements in the rapid development of digital economy?
Swift array map/flatmap/compactmap/filter/reduce/chaining Usage Summary
How to learn go language happily? Let's go!
Today, Tencent safety and SAIC Group officially announced!
Hook graphics kernel subsystem
How to convert XML to HL7
How to save data to the greatest extent after deleting LV by misoperation under AIX?