leetcode算法题--旋转链表
原题链接: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算法题--旋转链表相关推荐
- leetcode算法题--相交链表
原题链接: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ https://leetcode-cn.com/pro ...
- leetcode算法题--分隔链表
原题链接:https://leetcode-cn.com/problems/split-linked-list-in-parts/ vector<ListNode*> splitListT ...
- leetcode算法题--排序链表★
原题链接:https://leetcode-cn.com/problems/sort-list/ 1.归并排序(递归版) ListNode* sortList(ListNode* head) {if( ...
- leetcode算法题--重排链表★
原题链接:https://leetcode-cn.com/problems/reorder-list/ 1.map void reorderList(ListNode* head) {map<i ...
- leetcode算法题--环形链表 II★
原题链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/ 1.map ListNode *detectCycle(ListNode *he ...
- leetcode算法题--有序链表转换二叉搜索树★
原题链接:https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ 1.二分法+递归 TreeNode* ...
- leetcode算法题--反转链表 II★
原题链接:https://leetcode-cn.com/problems/reverse-linked-list-ii/ 1.头插法 ListNode* reverseBetween(ListNod ...
- leetcode算法题--分隔链表★
原题链接:https://leetcode-cn.com/problems/partition-list/ 1.双指针 ListNode* partition(ListNode* head, int ...
- leetcode算法题--删除链表的倒数第N个节点
原题链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/ 双指针法 ListNode* removeNthFrom ...
最新文章
- live555源码分析----RSTPServer创建过程分析
- [USACO07NOV]牛继电器Cow Relays
- 学java的困惑_学习Java - 关于一些代码困惑
- 关于使用 Python 析构函数的正确姿势
- 前端工程师技能之photoshop巧用系列扩展篇——自动切图
- it : Tmaster (hook declined) error: failed to push some refs to https://xxx/biluo/xxx.git
- D3 插入删除元素元素
- .NET深入学习笔记(2):C#中判断空字符串的4种方法性能比较与分析
- python爬虫常用模块介绍(1)_python爬虫常用模块介绍(1)
- oracle 监听 开机启动命令,如何让oracle DB、监听和oem开机启动(dbstart)
- application等对象的使用及监听器过滤器
- 网络空间安全现状与国家战略
- 92_目标:2019年底博客访问量达到10W+
- 实验三 类与对象的定义和使用
- (Rock, Paper, Scissors, Lizard and Spock)sheldon谢耳朵独创的剪刀,布,石头,蜥蜴,史波克 用代码实现
- ABAP 动态控制选择屏幕 / Free Selection
- 浅谈SAP FICO—总账科目
- 为什么有的人意志力非常好--自控力
- Landsat系列数据级别详解
- linux 查看磁盘信息
热门文章
- python文件的后缀名-python文件后缀是什么
- python文件输出-Python 文件和输入输出小结
- python画圆简单代码-Python画直线 画圆 画矩形代码
- python实训项目-黑马程序员上海校区Python21期Django项目实训
- python基础菜鸟教程-菜鸟教程学习python
- 罗德里格斯公式推导,以及如何使用cv2.Rodrigues进行旋转矩阵和旋转向量之间的相互转化
- LeetCode Contains Duplicate II(hash)
- QTableView中点击单元格弹出QComboBox
- HDU1715 大菲波数(大数相加)(Java题解)
- 将python文件打包成exe可运行文件