[Linked List]Rotate List

Total Accepted: 55428 Total Submissions: 250727 Difficulty: Medium

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

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

(E) Rotate Array

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    int getListLength(ListNode* head)
    {
        int len = 0;
        while(head){
            len++;
            head = head->next;
        }
        return len;
    }
    ListNode* rotateRight(ListNode* head, int k) {
        int len = getListLength(head);
        k = len ==0 ?  0 : len - k%len ;
        if(head ==NULL || k==0 || k==len){
            return head;
        }
        ListNode *newHead = NULL;
        ListNode *cur     = head;
        int cnt =0 ;
        while(cur){
            ++cnt;
            if(cnt == k){
                newHead = cur->next;
                cur->next=NULL;
                break;
            }
            cur = cur->next;
        }
        ListNode* p=newHead;
        while(p && p->next){
            p = p->next;
        }
        if(p){
            p->next=head;
        }
        return newHead;
    }
};

Next challenges: (M) Remove Duplicates from Sorted List II (H) Copy List with Random Pointer (M) Reorder List

时间: 2024-10-25 15:02:19

[Linked List]Rotate List的相关文章

LeetCode[Linked list]: Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 看到这个题目感到奇怪的是为什么是"右旋",而不是"左旋"呢?那么,左旋与右旋有什么区别呢?首

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

19.2.9 [LeetCode 61] Rotate List

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

力扣 — Rotate List()

题目描述: 中文: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 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 示例 2: 输入: 0->1->2

[LC] 61. Rotate List

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

leetcode61. Rotated List

问题描述: Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2-&g

Jan 19 - Rotate List; Linked List; Two Pointers; Iteration & Recursion;

Use 3 pointers each of them points to the address of Head, The node before Tail and Tail node; When rotating the list step by step, tail.next = head; tail_prev.next = null. New head = tail and new Tail = tail_prev; Code: (Recursion way) /** * Definit

leetcode 【Rotate List 】python 实现

题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 代码:oj在线测试通过 Runtime: 200 ms 1 # Definition for singly-linked lis

63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the va