博客
关于我
leetcode 61旋转链表 java双100
阅读量:138 次
发布时间:2019-02-27

本文共 516 字,大约阅读时间需要 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/

你可能感兴趣的文章
Neo4j(1):图数据库Neo4j介绍
查看>>
Neo4j(2):环境搭建
查看>>
Neo4j(3):Neo4j Desktop安装
查看>>
Neo4j(4):Neo4j - CQL使用
查看>>
Neo图数据库与python交互
查看>>