saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 온라인저지
  • 리트코드
  • LeetCode
  • c++
  • two pointer
  • 백준
  • 개발자 취업준비
  • 딕셔너리
  • 딥러닝
  • 알고리즘문제해결
  • 개발자
  • 취준
  • 문제해결능력
  • 취업준비
  • Python
  • 파이썬
  • BFS
  • DFS
  • 릿코드
  • 알고리즘

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

[LeetCode] 328. Odd Even Linked List
컴퓨터공학/LeetCode 1000

[LeetCode] 328. Odd Even Linked List

2022. 12. 7. 03:41

328. Odd Even Linked List

Medium

Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.

The first node is considered odd, and the second node is even, and so on.

Note that the relative order inside both the even and odd groups should remain as it was in the input.

You must solve the problem in O(1) extra space complexity and O(n) time complexity.

 

Example 1:

Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]

Example 2:

Input: head = [2,1,3,5,6,4,7]
Output: [2,3,6,7,1,5,4]

 

Constraints:

  • The number of nodes in the linked list is in the range [0, 104].
  • -106 <= Node.val <= 106

문제 풀이

  • 링크드 리스트가 주어진다.
  • 짝수 번째의 노드들을 모두 연결하고 홀수 번째의 노드들을 모두 연결한다.
  • 맨앞에 홀수 번째로 구성된 링크드리스트가 있어야하며 그 뒤에는 짝수 번째의 노드들이 연결되어야한다.
  • 공간 복잡도는 O(1) 상수 크기 만큼만 사용하고 시간 복잡도는 O(n)로 풀라고 나와있다. 
  • 홀수 번째 노드들을 각각 연결해나가면서, 짝수 번째 노드들도 연결한다.
  • 마지막에 짝수 번째의 헤드를 모든 과정이 끝나고 홀수 번째 노드 끝에 연결한다. 

소스 코드

# 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 == None: return head
        odd = head
        even = head.next
        evenhead = even
        while even != None and even.next != None:
            odd.next = even.next
            odd = odd.next
            even.next = odd.next
            even = even.next
        odd.next = evenhead
        return head
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 323. Number of Connected Components in an Undirected Graph  (0) 2022.12.08
[LeetCode] 2256. Minimum Average Difference  (0) 2022.12.07
[LeetCode] 1165. Single-Row Keyboard  (0) 2022.12.02
[LeetCode] 1099. Two Sum Less Than K  (1) 2022.12.02
[LeetCode] 1704. Determine if String Halves Are Alike  (0) 2022.12.02
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 323. Number of Connected Components in an Undirected Graph
    • [LeetCode] 2256. Minimum Average Difference
    • [LeetCode] 1165. Single-Row Keyboard
    • [LeetCode] 1099. Two Sum Less Than K
    saurus2
    saurus2
    Simple is Best

    티스토리툴바