<LeetCode 문제>
https://leetcode.com/problems/odd-even-linked-list/
Odd Even Linked 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
<풀이>
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None:
return None
odd = head
even = head.next
even_head = head.next
#반복하면서 홀짝 노드 처리
while even and even.next:
odd.next, even.next = odd.next.next,even.next.next
odd, even = odd.next,even.next
#홀수 노드의 마지막을 짝수 헤드로 연결
odd.next = even_head
return head
<학습내용>
1.
<학습이 필요한 내용>
1.
'Algorithm > 파이썬 알고리즘 인터뷰 스터디' 카테고리의 다른 글
| [스택, 큐] 20 유효한 괄호(Valid Parentheses) (0) | 2021.08.10 |
|---|---|
| [연결리스트] 19 역순 연결 리스트 II(Reverse Linked List II) (0) | 2021.08.09 |
| [연결리스트] 17 페어의 노드 스왑(Swap Nodes in Pairs) (0) | 2021.08.09 |
| [연결리스트] 16 두 수의 덧셈(Reverse Linked List) (0) | 2021.08.09 |
| [연결리스트] 15 역순 연결 리스트(Reverse Linked List) (0) | 2021.08.09 |