본문 바로가기
Programming/Algorithm

[leetcode] RotateList

by 읽고 쓰는 개발자 2020. 11. 29.

https://leetcode.com/problems/rotate-list/

 

Rotate List - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

import exam.ListNode;
public class RotateList {
    public static void main(String[] args) {
//        Input: 1->2->3->4->5->NULL, k = 2
        ListNode node5 = new ListNode(5);
        ListNode node4 = new ListNode(4, node5);
        ListNode node3 = new ListNode(3, node4);
        ListNode node2 = new ListNode(2, node3);
        ListNode node1 = new ListNode(1, node2);

        ListNode head = rotateRight(node1, 0);

        while(true) {
            System.out.println(head.val);
            if(head.next != null) {
                head = head.next;
            } else break;
        }

    }

    public static ListNode rotateRight(ListNode head, int k) {
        if(head == null) return head;
        ListNode answer ;
        ListNode next = head;
        int length = 0;
        while(true) {
            length++;
            if(next.next != null) {
                next = next.next;
            } else break;
        }

//       if k = 2 , The head goes back 2 steps
        k = k % length ;

//        No change
        if(k ==  0) return head;
//         etc
        next.next= head;
        next = head;
        for(int i = 0 ; i < length - k - 1; i++) {
            next = next.next;
        }
        answer = next.next;
        next.next = null;

        return answer;
    }
}

'Programming > Algorithm' 카테고리의 다른 글

[leetcode] ValidParentheses  (0) 2020.11.29
[leetcode] ValidateBinarySearchTree  (0) 2020.11.29
[leetcode] RomanToInt  (0) 2020.11.29
[leetcode] PopulatingNextRightPointersinEachNode  (0) 2020.11.29
[leetcode] Permutations  (0) 2020.11.29