In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how Python rotates the linked list to the right". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
For example:
Given
1-> 2-> 3-> 4-> 5-> NULL and k = 2
Return
4-> 5-> 1-> 2-> 3-> NULL
Topic: given a linked list, rotate the linked list to the right by k (non-negative) nodes.
Interpretation: I always feel that there is something wrong with the expression of this topic. I understand it this way to get to the point: keep the k nodes, such as the kink 2 given by example, then keep 4-> 5, and the first three nodes 1-> 2-> 3 move right to the back to form a new linked list 4-> 5-> 1-> 2-> 3. In addition, the k value may also be larger than the length of the linked list, which needs to be taken to obtain the real displacement.
Language: c
/ * Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode* next; *}; * / struct ListNode* rotateRight (struct ListNode* head, int k) {int index = 0tint listlength = 1bot struct ListNode* newlist = (struct ListNode*) malloc (sizeof (struct ListNode)); struct ListNode* tail = (struct ListNode*) malloc (sizeof (struct ListNode)); if (head = = NULL) {return NULL;} tail = head; newlist = head While (tail- > next) {/ / calculate the length of the linked list tail = tail- > next; listlength++;} k = k% listlength; / / k may be greater than the length of the linked list if (k = = 0) {/ / k equals the length of the linked list, do not rotate return head;} k = listlength-k / / move the front listlength-k nodes to the right, and the last k nodes do not move tail- > next = head; / / the tail node connects the first node for (index; index)
< k-1; index++){ //找都新链表头newlist = newlist->Next;} head = newlist- > next; / / New chain header newlist- > next = NULL; / / end of the linked list assignment NULLreturn head; / / return link header node}
Language: cpp
/ * Definition for singly-linked list. * struct ListNode {* int val; * ListNode* next; * ListNode (int x): val (x), next (NULL) {} *}; * / class Solution {public: ListNode* rotateRight (ListNode* head, int k) {if (head = = NULL) {return NULL;} ListNode* tail = head;int listlength = 1x while (tail- > next) {/ calculate the list length tail = tail- > next Listlength++;} k = k% listlength; / / k may be greater than the length of the linked list if (k = = 0) {/ / k equals the length of the linked list, do not rotate return head;} k = listlength-k; / / move the previous listlength-k nodes to the right, and the last k nodes remain unchanged tail- > next = head / / the tail node connects the head node ListNode * newlist = head;for (int index = 0; index)
< k-1; index++){ //找都新链表头newlist = newlist->Next;} head = newlist- > next; / / New chain header newlist- > next = NULL; / / end of the linked list assignment NULLreturn head; / / return link header node}}
Language:python
# Definition for singly-linked list.# class ListNode (object): # def _ _ init__ (self, x): # self.val = x # self.next = Noneclass Solution (object): def rotateRight (self, head K): ": type head: ListNode: type k: int: rtype: ListNode"if not head:return Nonetail = head listlength = 1while tail.next: tail = tail.next listlength + = 1k = k% listlengthif k = = 0:return head k = listlength-k tail.next = head newlist = headfor each in range (k-1): Newlist = newlist.next head = newlist.next newlist.next = Nonereturn head, "how to rotate the linked list to the right" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.