Rotating linked list of Python data structure

Source: Internet
Author: User
This article mainly introduces Python data structure-related information for rotating linked lists. if you need to rotate a linked list, you can refer to the following topic description: given a linked list, rotating the linked list allows each node to move k places to the right, k is a non-negative number.

Example: the linked list is 1-> 2-> 3-> 4-> 5-> null and k = 2; returns 4-> 5-> 1-> 2-> 3-> null.

First, let's take a look at the purpose of this question. In fact, in other words, we can describe it as follows: Give a K value, move the part after the chain table starts from the last k nodes to the front of the chain table. for example, the part 4-> 5 is actually moved to the front of the whole chain table, to 4-> 5-> 1-> 2-> 3-> null. However, note that the question does not show the size of k. when k is larger than the length of the linked list, we need to evaluate the remainder of the length of the linked list with k. for example, if k is 7, the above example moves 4-> 5 to the front of the entire linked list.

Therefore, the idea of this question can be summarized as follows:

1. first obtain the length of the entire linked list
2. find the precursor of the linked list to be moved based on the k value (3 in the example)
3. disconnect the linked list after the precursor and move the second half

The code is as follows:

# Definition for singly-linked list. # class ListNode: # def _ init _ (self, x): # self. val = x # self. next = None class Solution: # @ param head: the list # @ param k: rotate to the right k places # @ return: the list after rotation def rotateRight (self, head, k): if head is None: return head cur = head count = 1 # calculate the length of the linked list while cur. next: cur = cur. next count + = 1 # to save the amount of code, here is a very skillful solution: Use the end node chain connector node cur. ne Xt = head # Here, k is the number of steps required for cur to start from the end node to the part to be disconnected k = count-k % count # Find the precursor while k! = 0: cur = cur. next k-= 1 # disconnect head = cur. next cur. next = None # because the beginning and end of the node are connected, you can directly return the node following the precursor. here we reference it as head return head # write your code here

It should be noted that the 21 lines are connected at the beginning and end, which greatly saves the amount of our code. In fact, it is okay to follow the steps described in the previous ideas. But this technique is really great and worth learning. I wrote the details in the code comments.

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more articles about the rotating linked list of Python data structures, refer to PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.