61. Rotate List(Linked List-Medium)

本文介绍了一种链表操作算法——链表旋转,并提供了C、C++及Python三种语言的实现方式。该算法根据指定的数值k,将链表向右旋转k个位置,通过实例详细解释了算法的工作原理及注意事项。

转载请注明作者和出处:http://blog.csdn.net/c406495762

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

题目: 给定一个链表,通过k(非负)节点将链表旋转到右侧。

解读: 一直感觉这个题目的表述有些问题。我是这样理解才做对题的:保留后k个节点,比如example给出的k=2,那么保留4->5,前面的3个节点1->2->3右移到后面,形成新链表4->5->1->2->3。除此之外,k值也有可能是大于链表长度的,需要取余,获得真实的位移。

Language : c

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* rotateRight(struct ListNode* head, int k) {
    int index = 0;
    int listlength = 1;
    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){              //计算链表长度
        tail = tail->next;
        listlength++;
    }
    k = k % listlength;             //k可能大于链表的长度
    if(k == 0){                     //k等于链表长度,不旋转
        return head;
    }
    k = listlength - k;             //移动前面listlength-k个节点到右侧,后k个节点不动
    tail->next = head;              //尾节点连接首节点
    for(index; index < k-1; index++){      //找都新链表头
        newlist = newlist->next;
    }
    head = newlist->next;           //新链表头
    newlist->next = NULL;           //链表结尾赋值NULL
    return head;                    //返回链表头结点
}

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 = 1;
        while(tail->next){              //计算链表长度
            tail = tail->next;
            listlength++;
        }
        k = k % listlength;             //k可能大于链表的长度
        if(k == 0){                     //k等于链表长度,不旋转
            return head;
        }
        k = listlength - k;             //移动前面listlength-k个节点到右侧,后k个节点不动
        tail->next = head;              //尾节点连接首节点
        ListNode *newlist = head;
        for(int index = 0; index < k-1; index++){      //找都新链表头
            newlist = newlist->next;
        }
        head = newlist->next;           //新链表头
        newlist->next = NULL;           //链表结尾赋值NULL
        return head;                    //返回链表头结点
    }
};

Language:python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if not head:
            return None
        tail = head
        listlength = 1
        while tail.next:
            tail = tail.next
            listlength += 1
        k = k % listlength
        if k == 0:
            return head
        k = listlength - k
        tail.next = head
        newlist = head
        for each in range(k - 1):
            newlist = newlist.next
        head = newlist.next
        newlist.next = None
        return head

LeetCode题目汇总: https://github.com/Jack-Cherish/LeetCode

(pointcloud) root@autodl-container-07df49bbbc-5bf70353:~# jupyter notebook [I 2025-10-22 21:57:45.031 ServerApp] jupyter_lsp | extension was successfully linked. [I 2025-10-22 21:57:45.035 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2025-10-22 21:57:45.040 ServerApp] jupyterlab | extension was successfully linked. [I 2025-10-22 21:57:45.044 ServerApp] notebook | extension was successfully linked. [I 2025-10-22 21:57:45.342 ServerApp] notebook_shim | extension was successfully linked. [I 2025-10-22 21:57:45.366 ServerApp] notebook_shim | extension was successfully loaded. [I 2025-10-22 21:57:45.368 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2025-10-22 21:57:45.370 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2025-10-22 21:57:45.373 LabApp] JupyterLab extension loaded from /root/miniconda3/envs/pointcloud/lib/python3.9/site-packages/jupyterlab [I 2025-10-22 21:57:45.373 LabApp] JupyterLab application directory is /root/miniconda3/envs/pointcloud/share/jupyter/lab [I 2025-10-22 21:57:45.373 LabApp] Extension Manager is 'pypi'. [I 2025-10-22 21:57:45.429 ServerApp] jupyterlab | extension was successfully loaded. [I 2025-10-22 21:57:45.433 ServerApp] notebook | extension was successfully loaded. [I 2025-10-22 21:57:45.434 ServerApp] The port 8888 is already in use, trying another port. [C 2025-10-22 21:57:45.434 ServerApp] Running as root is not recommended. Use --allow-root to bypass. (pointcloud) root@autodl-container-07df49bbbc-5bf70353:~# jupyter notebook --port=8889 [I 2025-10-22 21:59:38.599 ServerApp] jupyter_lsp | extension was successfully linked. [I 2025-10-22 21:59:38.603 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2025-10-22 21:59:38.608 ServerApp] jupyterlab | extension was successfully linked. [I 2025-10-22 21:59:38.612 ServerApp] notebook | extension was successfully linked. [I 2025-10-22 21:59:38.879 ServerApp] notebook_shim | extension was successfully linked. [I 2025-10-22 21:59:38.900 ServerApp] notebook_shim | extension was successfully loaded. [I 2025-10-22 21:59:38.903 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2025-10-22 21:59:38.904 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2025-10-22 21:59:38.907 LabApp] JupyterLab extension loaded from /root/miniconda3/envs/pointcloud/lib/python3.9/site-packages/jupyterlab [I 2025-10-22 21:59:38.907 LabApp] JupyterLab application directory is /root/miniconda3/envs/pointcloud/share/jupyter/lab [I 2025-10-22 21:59:38.907 LabApp] Extension Manager is 'pypi'. [I 2025-10-22 21:59:38.961 ServerApp] jupyterlab | extension was successfully loaded. [I 2025-10-22 21:59:38.966 ServerApp] notebook | extension was successfully loaded. [C 2025-10-22 21:59:38.966 ServerApp] Running as root is not recommended. Use --allow-root to bypass. (pointcloud) root@autodl-container-07df49bbbc-5bf70353:~# jupyter notebook --port=9999 [I 2025-10-22 21:59:55.541 ServerApp] jupyter_lsp | extension was successfully linked. [I 2025-10-22 21:59:55.544 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2025-10-22 21:59:55.547 ServerApp] jupyterlab | extension was successfully linked. [I 2025-10-22 21:59:55.550 ServerApp] notebook | extension was successfully linked. [I 2025-10-22 21:59:55.723 ServerApp] notebook_shim | extension was successfully linked. [I 2025-10-22 21:59:55.739 ServerApp] notebook_shim | extension was successfully loaded. [I 2025-10-22 21:59:55.741 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2025-10-22 21:59:55.742 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2025-10-22 21:59:55.744 LabApp] JupyterLab extension loaded from /root/miniconda3/envs/pointcloud/lib/python3.9/site-packages/jupyterlab [I 2025-10-22 21:59:55.744 LabApp] JupyterLab application directory is /root/miniconda3/envs/pointcloud/share/jupyter/lab [I 2025-10-22 21:59:55.744 LabApp] Extension Manager is 'pypi'. [I 2025-10-22 21:59:55.781 ServerApp] jupyterlab | extension was successfully loaded. [I 2025-10-22 21:59:55.784 ServerApp] notebook | extension was successfully loaded. [C 2025-10-22 21:59:55.784 ServerApp] Running as root is not recommended. Use --allow-root to bypass. (pointcloud) root@autodl-container-07df49bbbc-5bf70353:~# jupyter notebook --port=8899 [I 2025-10-22 22:00:07.210 ServerApp] jupyter_lsp | extension was successfully linked. [I 2025-10-22 22:00:07.213 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2025-10-22 22:00:07.216 ServerApp] jupyterlab | extension was successfully linked. [I 2025-10-22 22:00:07.219 ServerApp] notebook | extension was successfully linked. [I 2025-10-22 22:00:07.412 ServerApp] notebook_shim | extension was successfully linked. [I 2025-10-22 22:00:07.428 ServerApp] notebook_shim | extension was successfully loaded. [I 2025-10-22 22:00:07.430 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2025-10-22 22:00:07.431 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2025-10-22 22:00:07.433 LabApp] JupyterLab extension loaded from /root/miniconda3/envs/pointcloud/lib/python3.9/site-packages/jupyterlab [I 2025-10-22 22:00:07.433 LabApp] JupyterLab application directory is /root/miniconda3/envs/pointcloud/share/jupyter/lab [I 2025-10-22 22:00:07.433 LabApp] Extension Manager is 'pypi'. [I 2025-10-22 22:00:07.470 ServerApp] jupyterlab | extension was successfully loaded. [I 2025-10-22 22:00:07.473 ServerApp] notebook | extension was successfully loaded. [C 2025-10-22 22:00:07.474 ServerApp] Running as root is not recommended. Use --allow-root to bypass.都不行呀
最新发布
10-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack-Cui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值