问题描述:

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.

算法分析:这个k是可以超过链表长度的,所以要对k取模。

public ListNode rotateRight(ListNode head, int k) {
int lenth = 0;//链表长度
ListNode headd = head;
while (headd != null) {
lenth++;
headd = headd.next;
}
if (head == null || head.next == null || k % lenth == 0) {
return head;
} ListNode headc = head;
for (int i = 1; i < lenth - k % lenth; i++) {
headc = headc.next;
}
ListNode newHead = headc.next;
ListNode p = newHead;
while (p.next != null) {
p = p.next;
}
p.next = head;
headc.next = null; return newHead;
}

最新文章

  1. makfile
  2. tomcat 和servlet之间的关系
  3. rhel7初体验
  4. &lt;input type=&quot;file&quot;&gt;中怎设置那个按钮的样式
  5. pycharm如何调试scrapy
  6. pyhton与json,Xml
  7. SQL Server 2008 表值参数用法
  8. 让项目管理理论&ldquo;落地&rdquo;&mdash;&mdash;读《IT项目经理成长手记》有感
  9. WCF之契约
  10. 第二篇、微信程序尺寸rpx
  11. 创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来
  12. ServletInvocableHandlerMethod:167 - Error resolving argument
  13. 【转】ASP.NET MVC教程
  14. chapter 10 统计检验
  15. Mybatis基础学习(五)&mdash;缓存
  16. Windows上安装scapy
  17. C++中的const总结
  18. Redis闪退解决办法
  19. Oracle 18C DBCA建库报ora-01012错误
  20. django前篇

热门文章

  1. mybatis的oracle的in超过1000的写法
  2. HYSBZ 2160 拉拉队排练(回文树)
  3. 并发编程5 操作系统&amp;进程
  4. 【Python之路】第十四篇--jQuery
  5. PHP去除所有的空格
  6. 深入理解Redis主键失效原理及实现机制(转)
  7. DP专题&#183;二
  8. Soap 教程
  9. HDU - 6397 Character Encoding 2018 Multi-University Training Contest 8 (容斥原理)
  10. hadoop08---读写锁