티스토리 뷰
Problem
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Example 1:
Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]
Example 2:
Input: head = [2,1], x = 2
Output: [1,2]
Constraints:
- The number of nodes in the list is in the range [0, 200].
- -100 <= Node.val <= 100
- -200 <= x <= 200
Solution
연결 리스트의 head 노드와 값 x가 주어질 때, x보다 작은 모든 노드가 x보다 크거나 같은 노드 앞에 오도록 분할하는 문제입니다.
이 때 기존의 상대적인 순서는 유지되어야 합니다.
package io.lcalmsky.leetcode.partition_list;
import io.lcalmsky.leetcode.ListNode;
public class Solution {
public ListNode partition(ListNode head, int x) {
// (1)
ListNode beforeHead = new ListNode(0);
ListNode before = beforeHead;
ListNode afterHead = new ListNode(0);
ListNode after = afterHead;
// (2)
while (head != null) {
// (3)
if (head.val < x) {
before.next = head;
before = before.next;
} else {
// (4)
after.next = head;
after = after.next;
}
// (5)
head = head.next;
}
// (6)
after.next = null;
// (7)
before.next = afterHead.next;
// (8)
return beforeHead.next;
}
}
- 두 개의 포인터를 생성합니다.
- 연결 리스트를 순차적으로 탐색하면서
- x보다 작은 경우 before 리스트에 추가합니다.
- x보다 크거나 같은 경우 after 리스트에 추가합니다.
- 다음 노드로 이동합니다.
- after 리스트의 다음 노드를 null로 설정하여 연결을 끊어줍니다.
- before 리스트의 마지막을 after 리스트의 head와 연결합니다.
- before 리스트의 head를 반환합니다.
Test
package io.lcalmsky.leetcode.partition_list;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import io.lcalmsky.leetcode.ListNode;
import org.junit.jupiter.api.Test;
class SolutionTest {
@Test
void testAll() {
assertAll(
() -> test(ListNode.of(1, 4, 3, 2, 5, 2), 3, ListNode.of(1, 2, 2, 4, 3, 5)),
() -> test(ListNode.of(2, 1), 2, ListNode.of(1, 2))
);
}
private void test(ListNode given, int x, ListNode expected) {
// when
Solution solution = new Solution();
ListNode actual = solution.partition(given, x);
// then
assertEquals(expected, actual);
}
}
'Algorithm > LeetCode' 카테고리의 다른 글
890. Find and Replace Pattern (0) | 2022.07.30 |
---|---|
34. Find First and Last Position of Element in Sorted Array (0) | 2022.07.29 |
92. Reverse Linked List II (0) | 2022.07.22 |
473. Matchsticks to Square (0) | 2022.07.16 |
746. Min Cost Climbing Stairs (0) | 2022.07.15 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 함께 자라기
- @ManyToOne
- Java
- spring boot app
- Spring Boot
- JPA
- Linux
- 스프링부트
- spring boot application
- leetcode
- Spring Data JPA
- JSON
- gRPC
- Spring Boot Tutorial
- QueryDSL
- 알고리즘
- proto3
- 스프링 부트 회원 가입
- Jackson
- 스프링 부트 튜토리얼
- 스프링 데이터 jpa
- spring boot jwt
- 스프링 부트
- 클린 아키텍처
- 스프링 부트 애플리케이션
- intellij
- r
- 함께 자라기 후기
- 헥사고날 아키텍처
- Spring Boot JPA
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함