原题链接:https://leetcode-cn.com/problems/rotate-list/

1、双指针法

相关题目:删除链表的倒数第N个节点

ListNode* rotateRight(ListNode* head, int k) {if(head==NULL) return head;ListNode *pre=new ListNode;pre->next=head;ListNode *p=head,*q=head;//p为前指针,q为后指针int size=0;while(q!=NULL){q=q->next;size++;}k=k%size;if(k==0) return head;//若k为0或者为链表长度,则直接返回链表q=head;while(k--){//p和q之前保持k距离q=q->next;}while(q->next!=NULL){//q一直走到最后一个链表结点p=p->next;q=q->next;}ListNode *tmp=p->next;//将(p:q]之间的节点移动到链表头部q->next=pre->next;pre->next=tmp;p->next=NULL;return pre->next;
}

2、闭合成环法

如上图(来源)

ListNode* rotateRight(ListNode* head, int k) {//将链表连成环,然后找到断开点断开,并设置头和尾。if(head==NULL) return head;ListNode *p=head;int size=1;while(p->next!=NULL){p=p->next;size++;}k=k%size;if(k==0)return head;p->next=head;int count=size-k;while(count--){p=p->next;//找到断开点}head=p->next;//断开p->next=NULL;return head;
}

leetcode算法题--旋转链表相关推荐

  1. leetcode算法题--相交链表

    原题链接: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ https://leetcode-cn.com/pro ...

  2. leetcode算法题--分隔链表

    原题链接:https://leetcode-cn.com/problems/split-linked-list-in-parts/ vector<ListNode*> splitListT ...

  3. leetcode算法题--排序链表★

    原题链接:https://leetcode-cn.com/problems/sort-list/ 1.归并排序(递归版) ListNode* sortList(ListNode* head) {if( ...

  4. leetcode算法题--重排链表★

    原题链接:https://leetcode-cn.com/problems/reorder-list/ 1.map void reorderList(ListNode* head) {map<i ...

  5. leetcode算法题--环形链表 II★

    原题链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/ 1.map ListNode *detectCycle(ListNode *he ...

  6. leetcode算法题--有序链表转换二叉搜索树★

    原题链接:https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ 1.二分法+递归 TreeNode* ...

  7. leetcode算法题--反转链表 II★

    原题链接:https://leetcode-cn.com/problems/reverse-linked-list-ii/ 1.头插法 ListNode* reverseBetween(ListNod ...

  8. leetcode算法题--分隔链表★

    原题链接:https://leetcode-cn.com/problems/partition-list/ 1.双指针 ListNode* partition(ListNode* head, int ...

  9. leetcode算法题--删除链表的倒数第N个节点

    原题链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/ 双指针法 ListNode* removeNthFrom ...

最新文章

  1. live555源码分析----RSTPServer创建过程分析
  2. [USACO07NOV]牛继电器Cow Relays
  3. 学java的困惑_学习Java - 关于一些代码困惑
  4. 关于使用 Python 析构函数的正确姿势
  5. 前端工程师技能之photoshop巧用系列扩展篇——自动切图
  6. it : Tmaster (hook declined) error: failed to push some refs to https://xxx/biluo/xxx.git
  7. D3 插入删除元素元素
  8. .NET深入学习笔记(2):C#中判断空字符串的4种方法性能比较与分析
  9. python爬虫常用模块介绍(1)_python爬虫常用模块介绍(1)
  10. oracle 监听 开机启动命令,如何让oracle DB、监听和oem开机启动(dbstart)
  11. application等对象的使用及监听器过滤器
  12. 网络空间安全现状与国家战略
  13. 92_目标:2019年底博客访问量达到10W+
  14. 实验三 类与对象的定义和使用
  15. (Rock, Paper, Scissors, Lizard and Spock)sheldon谢耳朵独创的剪刀,布,石头,蜥蜴,史波克 用代码实现
  16. ABAP 动态控制选择屏幕 / Free Selection
  17. 浅谈SAP FICO—总账科目
  18. 为什么有的人意志力非常好--自控力
  19. Landsat系列数据级别详解
  20. linux 查看磁盘信息

热门文章

  1. python文件的后缀名-python文件后缀是什么
  2. python文件输出-Python 文件和输入输出小结
  3. python画圆简单代码-Python画直线 画圆 画矩形代码
  4. python实训项目-黑马程序员上海校区Python21期Django项目实训
  5. python基础菜鸟教程-菜鸟教程学习python
  6. 罗德里格斯公式推导,以及如何使用cv2.Rodrigues进行旋转矩阵和旋转向量之间的相互转化
  7. LeetCode Contains Duplicate II(hash)
  8. QTableView中点击单元格弹出QComboBox
  9. HDU1715 大菲波数(大数相加)(Java题解)
  10. 将python文件打包成exe可运行文件