leetcode---rotate-list---链表
来源:互联网 发布:单片机一键信号发射 编辑:程序博客网 时间:2024/06/05 00:26
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given1->2->3->4->5->NULLand k =2,
return4->5->1->2->3->NULL.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *reverse(ListNode *head) { if(!head || !head->next) return head; ListNode *nextH = head->next; ListNode *newH = reverse(nextH); nextH->next = head; head->next = NULL; return newH; } ListNode *rotateRight(ListNode *head, int k) { if(!head || k == 0) return head; int n = 0; ListNode *p = head; ListNode *last = head; while(p) { n++; last = p; p = p->next; } k = k % n; if(k == 0) return head; n = n - k; int i = 1; p = head; while(p && i<n) { i++; p = p->next; } ListNode *next = p->next; p->next = NULL; last->next = head; return next; }};
阅读全文
0 0
- leetcode---Rotate List---链表
- leetcode---rotate-list---链表
- [leetcode] 【链表】 61. Rotate List
- [leetcode][list] Rotate List
- LeetCode: Rotate List
- LeetCode Rotate List
- LeetCode : Rotate List
- [Leetcode] Rotate List
- [LeetCode] Rotate List
- [leetcode] Rotate List
- leetcode 70: Rotate List
- [Leetcode] Rotate List
- 【leetcode】Rotate List
- [LeetCode]Rotate List
- [leetcode]Rotate List
- LeetCode-Rotate List
- [leetcode] Rotate List
- LeetCode - Rotate List
- recyclerview 点击事件
- 使用JQuery编写AJax实现异步请求
- Python读取xml文件(xml.etree.ElementTree)
- 基于ugui的血条(资料备份)
- 个人笔记
- leetcode---rotate-list---链表
- LintCode/LeetCode全排列系列问题--Permutations I 和 II, N-Queens I 和 II,数独问题
- thymeleaf的使用笔记
- 10月25日 c语言 读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*
- 实验报告四
- 使用RecyclerView实现购物车
- CircularReveal
- C语言练习题(2)
- android手机客户端测试体系