Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- LeetCode #
- 도메인 #도메인 주도 설계 #도메인 주도 설계 핵심 #DDD
- leetcode #2206. Divide Array Into Equal Pairs
- 코딩게임
- leetcode #알고리즘 #릿코드
- DDD #도메인 #도메인 주도 설계 #도메인 주도 섥계 핵심
- 도메인 주도 설계 핵심
- Find Pivot Index
- DDD #도메인 #도메인 주도 설계 #도메인 주도 설계 핵심
- leetcode #20. Valid Parentheses #알고리즘 #leetcode Valid Parentheses
- 반 버논
- #move zeroes
- base7
- leetcode
- 도메인 주도 설계 핵심 #DDD #도메인 주도 설계 #도메인
- #20. Valid Parentheses java
- codinGame
- Longest Substring WIthout Repeating Characters
- aws #cloudwatch #log insight
- codingame #코딩게임 #codingame fall challenge2023 #코딩게임 2023 가을 챌린지
- 867. Transpose Matrix #Transpose Matrix
- ddd
- Fall Challenge 2023
Archives
- Today
- Total
주하니 서하아빠
61. Rotate List 본문
Given the head of a linked list, rotate the list to the right by k places.
Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
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) {
if(head == null ) return head;
ListNode end = head;
int len = 1;
while(end.next !=null) {
end = end.next;
len++;
}
System.out.println("end.val: " + end.val + ", len:" + len);
int move = len - (k % len);
ListNode newHead = head;
ListNode newEnd = head;
for(int i=0; i< move -1; i++) {
newEnd = newEnd.next;
}
for(int i=0; i< move ; i++) {
newHead = newHead.next;
}
if(newHead != null) {
newEnd.next = null;
end.next = head;
}
return newHead == null ? head : newHead;
}
}
'알고리즘 > LeetCode' 카테고리의 다른 글
217. Contains Duplicate (0) | 2022.05.20 |
---|---|
25. Reverse Nodes in k-Group (0) | 2022.05.20 |
200. Number of Islands (0) | 2021.06.14 |
693. Binary Number with Alternating Bits (0) | 2021.04.20 |
283. Move Zeroes (0) | 2021.04.07 |