Leetcode-Rotate List
来源:互联网 发布:python能开发界面吗 编辑:程序博客网 时间:2024/06/07 08:12
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) { if(head == NULL || head->next == NULL||k==0) return head; ListNode* node = head; int size =1; while(node->next != NULL) { size++; node = node->next; } //loop the list node->next=head; //handle the case of k>size k = k%size; //find the node to break the loop at while(--size >= k) { node=node->next; } ListNode* first = node->next; node->next=NULL; return first; }};
阅读全文
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
- sublimText 安装插件方法
- 文章标题
- hibernate QBC criteria.createAlias()返回数据数组问题
- Finding the Radius for an Inserted Circle 笛卡尔定理
- Manthan, Codefest 17 C. Helga Hufflepuff's Cup
- Leetcode-Rotate List
- java对redis的基本操作
- C#之各类异同
- setTimeout中this的指向,使用箭头函数后this的指向
- 页面等待窗口
- bitmap原理
- oracle 之 循环、游标
- ORA-00979 不是GROUP BY表达式
- [笔记]tarjan-无向图(桥、割点、双联通分量)