博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 61旋转链表 java双100
阅读量:136 次
发布时间:2019-02-27

本文共 520 字,大约阅读时间需要 1 分钟。

class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head==null || k==0) {
return head; } ListNode cur = head;//假节点指头 ListNode tail = null;//尾 int length = 1; while(cur.next != null) {
cur = cur.next; length++; }//求长度 int num = length-(k%length); tail = cur;//指尾 cur.next = head;//改循环链表 cur = head; for(int i=0;i

本题说是循环旋转,但其实是将尾部向前数第K个元素作为头,原来的头接到原来的尾上,这应该也是一种更好的思路java双100

转载地址:http://mbyd.baihongyu.com/

你可能感兴趣的文章