KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • 그래프
  • 제코베 JS 100제
  • 정렬
  • string
  • C
  • Level1
  • programmers
  • Level 2
  • js
  • recursion
  • 문자열
  • Level 0
  • 수학
  • LeetCode
  • C++
  • tree
  • Level 1
  • 백준
  • 다이나믹 프로그래밍
  • codeup

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Linked List / 328번 / Odd Even Linked List / JS

2023. 4. 28. 17:16

< 문제 바로가기 >

 

Odd Even Linked List - LeetCode

Can you solve this real interview question? Odd Even Linked List - 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 od

leetcode.com

 

< 문제 간단설명 >

홀수번째 노드들을 앞으로 오게 하고, 짝수번째 노드들은 뒤로 보낸다.

 

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var oddEvenList = function(head) {
    if(!head) return head;

    // head의 맨 처음을 가리키도록
    let oddList = head;
    // head의 맨 처음노드의 다음을 가리키도록(2번째)
    let evenList = head.next;

    let oddListHead = oddList;
    let evenListHead = evenList;

    // 한칸씩 건너뛰면서 홀수와 짝수번째 각각 알맞게 next로 설정
    while(oddList.next && evenList.next) {
        oddList.next = oddList.next.next;
        oddList = oddList.next;

        evenList.next = evenList.next.next;
        evenList = evenList.next;
    }

    // 홀수번째 리스트 다음에 짝수번째 리스트가 오도록
    oddList.next = evenListHead;
    let result = oddListHead;

    return result;
};
저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Greedy / 2131번 / Longest Palindrome by Concatenating Two Letter Words / JS  (0) 2023.04.28
LeetCode / Linked List / 148번 / Sort List / JS  (0) 2023.04.28
LeetCode / Simulation / 1706번 / Where Will the Ball Fall / JS  (0) 2023.04.20
LeetCode / Linked List / 234번 / Palindrome Linked List / JS  (0) 2023.04.19
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Greedy / 2131번 / Longest Palindrome by Concatenating Two Letter Words / JS
    • LeetCode / Linked List / 148번 / Sort List / JS
    • LeetCode / Simulation / 1706번 / Where Will the Ball Fall / JS
    • LeetCode / Linked List / 234번 / Palindrome Linked List / JS
    KimMinJun
    KimMinJun

    티스토리툴바