61. Rotate List
来源:互联网 发布:网民网络应用 编辑:程序博客网 时间:2024/05/18 13:43
/** * 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;int count = 0;ListNode* L = new ListNode(0);ListNode* q = NULL, *p = NULL,*s=NULL;L->next = head;s =p= head;while (s != NULL)//计算链表的长度;{count++;s = s->next;}k=k%count;if (k==0)return head;for (int i = 0; i < count - k - 1; i++)//找到将要成为尾结点的结点;{p = p->next;}q = p->next;//将要成为首结点的结点;p->next = NULL;L->next = q;while (q ->next!= NULL)//寻找将要链接原首结点的结点;{q = q->next;}q->next = head;return L->next; }};
0 0
- [LeetCode]61.Rotate List
- 61. Rotate List
- LeetCode --- 61. Rotate List
- [Leetcode] 61. Rotate List
- 61. Rotate List
- [leetcode] 61.Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- leetcode 61. Rotate List
- LeetCode 61. Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List LeetCode
- leetcode 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 在O(1)时间复杂度删除链表节点
- js实现表格排序
- 蓝桥杯 基础练习 十六进制转八进制
- hdu1079 找规律/博弈
- 大话设计模式读书笔记(三) 单一职责原则和开放-封闭原则和依赖倒转原则
- 61. Rotate List
- poj 3692 二分图匹配
- (一)微信小程序开发配置
- 排列组合
- 广工ProblemH 1231 TMK买礼物(判定数组)
- MyISAM和InnoDB索引引擎的B+树索引实现
- MFC中字符之间转换
- 关于SPI的一点总结
- 利用tomcat发布webservice