找LeetCode上简单的题目来撑过30天啦(DAY6)

今天尝试了前几天被我放弃的题目,过一两天後果然比较有感觉,花了一点时间还是完成了呢,心情不错/images/emoticon/emoticon07.gif

题号:61 标题:Rotate List 难度:Medium

Given the head of a linked list, rotate the list to the right by k places.

Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]

Example 2:

Input: head = [0,1,2], k = 4
Output: [2,0,1]

Constraints:
• The number of nodes in the list is in the range [0, 500].
• -100 <= Node.val <= 100
• 0 <= k <= 2 * 109

我的程序码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        ListNode result = new ListNode();
        ListNode temp = new ListNode();
        ListNode v = new ListNode();
        int i=0,j=0,count=1;
        if(head== null || k==0 || head.next==null){
            return head;
        }
        
        for(i=0;i<k;i++){  
            while(true){
                if(head.next==null) {
                    temp = head;
                    break;
                }else if(j==0){
                    result = head;                
                    j++;
                }
                head =head.next;
                count++;
            }
            System.out.println(count);
            
            v = result;
            while(true){
                if(result.next==temp) {
                    result.next = null;
                    break;
                }
                result =result.next;      
            }   
            temp.next= v;
            j=0;
            head = temp;
            if(i==0){
                if(k/count>count){
                    k = k%count;
                }else if( k%count==0){
                    k = count;
                }
            }
        }
        return temp;
    }
}

花比较久的时间在
了解此题的Definition for singly-linked list,基本资料结构的概念是有的,但从来没有实际碰到过,所以花了点时间了解一下如何达成我要得目的,不能回头真的是好烦呀

DAY6心得
我对於内容越来越偷懒了,反正也是想让自己一直有在进步而已,应该OK吧?


<<:  DAY 06 Variable

>>:  如果你不规画自己的人生,那你很可能会落入别人的规画里。

老肝哥-菜鸟Java的LeetCode历程,第十三题:Roman to Integer,朝远大目标前进!

嘿嘿!各位好你最好的朋友老肝哥照惯例又来了 今天老肝哥心情其实不错 因为自己又坚持一天了,但老肝哥在...

< 关於 React: 开始打地基| 建立一个最小的单位 “Element” >

09-04-2021 避免与component混淆的最小单位 在建立element时,与DOM el...

[Lesson7] Activity生命周期

为了在Activity生命周期的各个阶段之间导航转换,Activity类提供六个核心回调:onCre...

[Day22] HTB Blue

URL : https://app.hackthebox.eu/machines/51 IP : ...

Day 05 : ML 专案生命周期

从无到有开发 ML 专案到布署需要 6 至 12 个月不等,在尚未有具体产出的过程中,会有对内部及...