상위 150개 인터뷰쉬움

K 그룹의 역방향 노드

'K 그룹의 역방향 노드' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

연결된 목록의 헤드가 주어지면 목록 k의 노드를 한 번에 반전시키고 수정된 목록을 반환합니다.

k는 양의 정수이고 연결된 목록의 길이보다 작거나 같습니다. 노드의 개수가 k의 배수가 아닌 경우, 남은 노드는 결국 그대로 유지되어야 합니다.

목록 노드의 값은 변경할 수 없으며 노드 자체만 변경할 수 있습니다.

연결된 목록은 Python 목록으로 표시됩니다. reverseKGroup(head: list, k: int) -> list 함수를 구현하세요.

제약
  • The number of nodes in the list is n
  • 1 <= k <= n <= 5000
  • 0 <= Node.val <= 1000

Example 1
Input
[1,2,3,4,5], 2
Output
[2,1,4,3,5]
Explanation

Reverse in groups of 2: [1,2] becomes [2,1], [3,4] becomes [4,3], and [5] stays as is.

Example 2
Input
[1,2,3,4,5], 3
Output
[3,2,1,4,5]
Explanation

Reverse in groups of 3: [1,2,3] becomes [3,2,1], and [4,5] has fewer than 3 nodes so it stays.

Need a Hint?
세트나 힙과 같은 연결 목록 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.