061. Rotate list

Source: Internet
Author: User

Link: https://leetcode.com/problems/rotate-list/description/

Example 1:

Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplanation:rotate 1 steps to the right: 5->1->2->3->4->NULLrotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:

Input: 0->1->2->NULL, k = 4Output: 2->0->1->NULLExplanation:rotate 1 steps to the right: 2->0->1->NULLrotate 2 steps to the right: 1->2->0->NULLrotate 3 steps to the right: 0->1->2->NULLrotate 4 steps to the right: 2->0->1->NULL

 

Ideas:

  • According to the question, a single-chain table and a non-negative integer are given.K, For the linked listKSecondary rotation operation. Each rotation operation inserts the End Node of the single-chain table before the current single-chain header node.
  • Because the insert operation in each rotation operation requires operations at the first node. To facilitate node insertion, we can construct a secondary node pointing to the first node of the linked list.
  • During each rotation operation, the operations we need can be dividedTwo steps:
    1. Step 1To locate the End Node of a single-chain table, a node pointer is used to point to the node before the end node (because the point of this node needs to be changed to nullptr, And the node becomes the end node );
    2. Step 1To insert the end node to the header node of a Single-linked table and make it the first node of the single-linked table.
  • Perform this operation K times in a loop.

  Note: If the encoding is implemented according to the operation described above, the time limit exceeded issue may occur.

The optimized part is to optimize the K-rotation operation. Assume that the number of linked list nodes is N, that is, the length of the linked list is N.

The number of rotation operations is loop = K % N;

Because the chain table rotation operation takes the length N of the chain table as the cycle, that is, the number of rotation operations should be less than the length N of the chain table.

Other optimizable parts can be further considered. Not finished yet !!!

In addition, pay attention to some small details in the encoding process. For example, when the number of nodes in a single-chain table is 1, the preceding step 1 may have a logic error (pointer pointing error ).

    

The encoding is as follows::

1/** 2 * definition for singly-linked list. 3 * struct listnode {4 * int val; 5 * listnode * Next; 6 * listnode (int x): Val (x), next (null) {} 7 *}; 8 */9 class solution {10 public: 11 listnode * rotateright (listnode * head, int K) {12 // The linked list is empty, 13 if (Head = nullptr) return head; 14 15 int CNT = 0; // count the number of linked list nodes 16 listnode * P = head; 17 while (P! = Nullptr) 18 {19 CNT ++; 20 p = p-> next; 21} 22 23 int loop = K % CNT; // The number of times to rotate 24 25 // secondary node, always pointing to the head node of the linked list 26 listnode * phead = new listnode (-1); 27 phead-> next = head; 28 29 listnode * pre = nullptr; // point to the first node of the node to be rotated 30 for (INT I = 0; I <loop; ++ I) // rotate 31 {32 listnode * ptemp = head; 33 while (ptemp-> next! = Nullptr) 34 {35 pre = ptemp; 36 ptemp = ptemp-> next; 37} 38 39 if (pre = nullptr) // Note: When the chain table length is 1, 40 {41 return head; 42} 43 44 Pre-> next = nullptr; // pre becomes the tail pointer to the End Node of the linked list 45 46 // Insert the searched node to the first 47 phead-> next = ptemp; 48 ptemp-> next = head; 49 head = ptemp; // head always points to the head node of the linked list 50} 51 52 return head; 53} 54 };

 

061. Rotate list

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.