[LeetCode]Rotate List
2015-08-19 18:14
316 查看
题目
Number: 61Difficulty: Medium
Tags: Linked List, Two Pointers
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
[code]Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.
题解
反转倒数第k个结点开头的链表。代码
[code]ListNode* rotateRight(ListNode* head, int k) { if(!head || !k || !head->next){ return head; } int n = 1; ListNode *p = head; while(p->next){ p = p->next; n++; } k %= n; n -= k; p->next = head; if(k){ while(n--){ p = p->next; } } head = p->next; p->next = NULL; return head; }
相关文章推荐
- Objective - C基础简单地初始化
- Apache2.2+Tomcat7.0整合配置详解
- 70分产品经理——数据驱动产品之路
- 关于“书是人类进步的阶梯”的一点点看法
- Introduction to Java Programming编程题5.32<赢取双骰子游戏的次数>
- Spring整合Quartz(JobDetailBean方式)
- 全局异常捕获
- MySQL添加用户、删除用户与授权
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
- poj1734
- python中的循环语句
- "Couldn't resolve host 'mirrors.aliyun.com'"
- 手机端网页设计尺寸
- bzoj1084
- java.util.concurrent包API学习笔记
- hdu 5178 pairs (线性探查问题)
- Centos磁盘的知识总结
- 过滤器(Filter)和拦截器(Interceptor)的区别
- 主流手机分辨率 尺寸 操作系统
- 常用软件下载地址汇总