当前位置:网站首页>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 :
边栏推荐
- 05. Tencent cloud IOT device side learning -- mqtt protocol client implementation
- 中金证券靠谱吗?是否合法?开股票账户安全吗?
- Zblog determines whether a plug-in installs the enabled built-in function code
- How to save data to the greatest extent after deleting LV by misoperation under AIX?
- 重新定义存储架构,华为用了不止5颗“芯”
- [2021 taac & Ti-One] FAQs related to preliminary round computing resources
- NFT元宇宙源码搭建解析与介绍
- Best practices for H5 page adaptation and wechat default font size
- Create a green city and 3D visualization of digital twin natural gas stations
- FPGA systematic learning notes serialization_ Day8 [design of 4-bit multiplier and 4-bit divider]
猜你喜欢
Why do you develop middleware when you are young? "You can choose your own way"
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)
MySQL learning -- table structure of SQL test questions
随机推荐
Solution to the problem that kibana's map cannot render longitude and latitude coordinate data
Tencent security officially released the IOT security capability map
FPGA systematic learning notes serialization_ Day9 [serial port printing of PS terminal of Xilinx zynq7000 series]
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)
This time, talk about the dry goods of industrial Internet | TVP technology closed door meeting
One article combs multi task learning (mmoe/ple/dupn/essm, etc.)
Introduction to koa (III) koa routing
Hook graphics kernel subsystem
Example description and case of ansible playbook automated cluster server management
IBM: supporting AI and enterprise digital reshaping in the cloud era with modern architecture
Pagoda activities, team members can enjoy a lightweight server 1 core 2g5m 28 yuan for two years
Sigai intelligent container damage identification products are deployed in Rizhao Port and Yingkou Port
[tke] whether to configure SNAT when the container accesses services outside the node
主链系统发展解析
Introduction to visual studio shortcut keys and advanced gameplay
In those years, I insisted on learning the motivation of programming
Introduction to website development for zero foundation Xiaobai
Cloud native monitoring configuration self built alertmanager to realize alarm
How important is it to document the project? I was chosen by the top 100 up leaders and stood up again
Snapshot management for elastic cloud enterprise