목록 재정렬
'재주문 목록' 문제에 대한 자세한 가이드 및 Python 구현입니다.
1. 배우다
'목록 재정렬' 문제는 연결 목록 섹션의 주요 과제입니다.
이 구현은 Python의 쉬운 수준 논리에 중점을 둡니다.
우리는 제공되는 솔루션에서 기술적 정확성과 코드 가독성을 최우선으로 생각합니다.
2. Real-World Applications
3. Visual Intuition
재주문 목록의 논리 흐름을 시각화합니다.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Reorder List의 문제 설명을 주의 깊게 읽어보세요.
2. Formulate brute force
간단한 반복 솔루션 초안을 작성합니다.
3. Identify inefficiency
중복 계산을 찾으십시오.
4. Optimize search path
해싱이나 정렬을 사용하여 프로세스 속도를 높입니다.
5. Final Implementation
생산 표준에 맞게 코드를 정리합니다.
문제 설명
단일 연결 리스트의 헤드가 제공됩니다. 목록은 다음과 같이 나타낼 수 있습니다.
L0 → L1 → … → Ln-1 → Ln
목록을 다음 형식으로 재정렬합니다.
L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …
목록 노드의 값은 수정할 수 없습니다. 노드 자체만 변경할 수 있습니다.
연결된 목록은 Python 목록으로 표시됩니다. 재정렬된 목록을 반환하는 reorderList(head: list) -> list 함수를 구현하세요.
- •The number of nodes in the list is in the range [1, 50000]
- •1 <= Node.val <= 1000
예
[1,2,3,4]
[1,4,2,3]
The list 1->2->3->4 is reordered to 1->4->2->3.
[1,2,3,4,5]
[1,5,2,4,3]
The list 1->2->3->4->5 is reordered to 1->5->2->4->3.
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.
인터뷰 통찰력 및 변형
복잡성 분석 분석
왜 시간인가?: Directly evaluates all possibilities.
왜 우주인가?: Uses standard local memory.
왜 시간인가?: Optimized paths reduce total operations.
왜 우주인가?: May trade memory for speed.
최적화된 솔루션 Python 코드
최적화된 솔루션 Python 코드
def reorder_list_opt(head):
if isinstance(head, list):
h = build_linked_list(head)
reorder_list_opt_helper(h)
return linked_list_to_list(h)
return reorder_list_opt_helper(head)
def reorder_list_opt_helper(head: ListNode) -> None:
if not head or not head.next:
return
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
prev, curr = None, slow.next
slow.next = None
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
first, second = head, prev
while second:
tmp1, tmp2 = first.next, second.next
first.next = second
second.next = tmp1
first, second = tmp1, tmp2무차별 대입 코드(스포일러 보호)
무차별 대입 코드(스포일러 보호)
def reorder_list_brute(head):
if isinstance(head, list):
h = build_linked_list(head)
reorder_list_brute_helper(h)
return linked_list_to_list(h)
return reorder_list_brute_helper(head)
def reorder_list_brute_helper(head: ListNode) -> None:
if not head or not head.next:
return
nodes = []
curr = head
while curr:
nodes.append(curr)
curr = curr.next
l, r = 0, len(nodes) - 1
while l < r:
nodes[l].next = nodes[r]
l += 1
if l == r:
break
nodes[r].next = nodes[l]
r -= 1
nodes[l].next = NoneAlgorithm Pattern Checklist
When dealing with Linked List data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
표준 연결 목록 문제 속성이 적용됩니다.
관련 질문
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
권장 Python 리소스
관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.
Python 목록
Python 목록에 대한 모든 것을 알아보세요. Python에서 배열을 기본적으로 생성, 분할, 수정 및 반복하는 방법을 알아보세요.
Python에서 목록을 정렬하는 방법(오름차순 및 내림차순)
sort() 메서드와 sorted() 함수를 사용하여 Python에서 목록을 정렬하는 방법을 알아보세요. 사용자 정의 키 정렬 및 역순 예시를 살펴보세요.
Python 목록 메서드 치트 시트
Python 목록 작업에 대한 빠른 참조 가이드입니다. 요소 추가, 삽입, 제거, 정렬 및 분할을 마스터합니다.
Python 대 JavaScript: 어떤 프로그래밍 언어가 가장 좋나요?
Python과 JavaScript를 포괄적으로 비교합니다. 구문 차이점, 성능, 사용 사례(백엔드와 프런트엔드) 및 코딩 예제를 살펴보세요.