leetcode-Rotate List
来源:互联网 发布:mac怎么下载穿越火线 编辑:程序博客网 时间:2024/06/05 02:27
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
.
/** * 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) { vector<int> a; ListNode *temp = head; while(temp) { a.push_back(temp->val); temp = temp->next; } int n = a.size(); temp = head; if(n) k = k%n; int m = n-k; while(temp) { int j = m%n; temp->val = a[j]; if(j == n-k-1)break; m++; temp = temp->next; } return head; }};
0 0
- [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
- LeetCode:Rotate List
- LeetCode 61: Rotate List
- leetcode之Rotate List
- [bash]read命令
- Android 关于post、get、pull、delete解析分析
- 实例学习gcc+gdb+make
- 【 Codeforces Round #268 (Div. 1)】B.Two Set【dfs找增广路】
- Linux下C语言实现查看进程是否存在
- leetcode-Rotate List
- A JSONObject text must begin with '{' at character 1 of 1
- Linux 下给项目搭建svn环境
- 数学之路-数据分析进阶-多变量数据分析(3)
- C#内存回收
- Entity Framework 并发处理
- Linux——线程同步
- 树莓派学习笔记(1):入手树莓派
- DRAM SRAM SDRAM区别