LeetCode 61: rotating list Rotate List

Given a list, the list of rotation, each node in the list is moved rightward k positions, wherein k is non-negative.

Given a linked list, rotate the list to the right by k places, where k is non-negative.

Example 1:

输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL

Example 2:

输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL

Problem-solving ideas:

If you read last week's article: LeetCode 189: rotating arrays , and this article rotating list in addition to carrying data structure has changed, the other are the same, simply click

Continue to reverse a specific length of the array:

Input: 1-> 2-> 3-> 4-> 5

Reverse entire array: 5-> 4-> 3-> 2-> 1

List before the split is a length of k bits: 5-> 4, 3-> 2-> 1

Rotating front k bits: 4-> 5

The remaining reverse: 1-> 2-> 3

And output connections: 4-> 5-> 1-> 2-> 3

According to this idea, the list can be reversed only three times, and we have reversed the list in the article LeetCode 206: reverse list has been described in detail in the past, using the iterative, recursive two ways, so in accordance with the above-mentioned solution of the problem the method can also be achieved in two ways.

The Solution as described above, the rotational similar array that question, to introduce another very simple and efficient way.

Observation of input and output:

Input: 1-> 2-> 3-> 4- > 5, k = 2
Output: 4-> 5-> 1-> 2-> 3

After cutting the linked list node 3: 1-> 2-> 3,4> 5

The head node is connected to the end node: 4-> 5-> 1-> 2-> 3

Output: 4-> 5-> 1-> 2-> 3

Thanks to the characteristics of the list, in this manner significantly simpler, and the position is just the node 3 len-k(len is the length of the list). At just len-kafter the cut node, end to end connection.

Further k may be greater than the length of the list, it will be a remainder operation k=k%len. Taking into account the cutting location may be the last node of a node, or a location 0 (i.e. before the head node), do not significantly cut node, but the above-described operation pointer overflow error occurs, can be the first node to end to end, form a ring chain .

Java:

class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k == 0) return head;
        int len = 1;
        ListNode cur = head;
        while (cur.next != null) {//计算链表长度
            cur = cur.next;
            len++;
        }
        cur.next = head;
        int mod = len - k % len;//切断节点的位置
        cur = head;
        while (--mod > 0) cur = cur.next;//找到切断节点
        ListNode newHead = cur.next;//新链表头节点
        cur.next = null;//切断
        return newHead;
    }
}

Python3:

class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if not head or not head.next or not k: return head
        cur = head
        len = 1
        while cur.next:
            len += 1
            cur = cur.next
        cur.next = head
        cur = head
        mod = len - k % len
        while mod - 1 > 0:
            cur = cur.next
            mod -= 1
        newHead = cur.next
        cur.next = None
        return newHead

.. Welcome attention to the micro-channel public learning together all the numbers: Love Bug write

I love to write Bug.png

Guess you like

Origin www.cnblogs.com/zhangzhe532/p/11248892.html