热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

rotatelistjava_RotateList|Java最短代码实现

【思路】1.将尾指针指向头指针2.并将head指针向后移动kcount–k%count-1位,pheadhead.next就是新链表的头指针3.将head.next置

【思路】

1.    将尾指针指向头指针

2.    并将head指针向后移动k = count – k % count - 1位,phead = head.next就是新链表的头指针

3.    将head.next置空,并返回phead

public ListNode rotateRight(ListNode head, int k) {

if (head == null) return head;

ListNode phead = head;

int count = 1;

while (phead.next != null) {

count++;

phead = phead.next;

}

k = count - k % count - 1;

phead.next = head;

while (k-- > 0)

head = head.next;

phead = head.next;

head.next = null;

return phead;

}

230 / 230test cases passed. Runtime:1 ms  Your runtime beats 14.33% of javasubmissions.

欢迎优化!



推荐阅读
author-avatar
手机用户2602901563
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有