Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • docker
  • 다이나믹 프로그래밍
  • DFS
  • BFS
  • boj
  • 백트래킹
  • django
  • Kubernetes
  • Network
  • 프로그래머스

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

[LeetCode] 328. Odd Even Linked List (C#)
Algorithm/LeetCode

[LeetCode] 328. Odd Even Linked List (C#)

2024. 5. 17. 20:59

https://leetcode.com/problems/odd-even-linked-list/description/

 

 

 

홀수번째 Node를 앞으로 당겨오고, 짝수번째 Node를 뒤로 밀어주는 문제.

 

제한 사항

  • 공간 복잡도 : O(1)
  • 시간 복잡도 : O(N)

 

1. 풀이

  • 더 이상 Odd 노드가 없을 때까지 Odd는 Odd끼리, Even은 Even끼리 연결
  • 마지막에 Odd의 tail과 Even의 head를 연결
namespace PS.LeetCode;

public class Solution_328_1
{
    public class ListNode
    {
        public int val;
        public ListNode next;

        public ListNode(int val = 0, ListNode next = null)
        {
            this.val = val;
            this.next = next;
        }
    }
    
    public ListNode OddEvenList(ListNode head)
    {
        if (head == null)
        {
            return head;
        }
        
        var oddNode = head;
        var evenHead = head.next;
        
        while (oddNode.next?.next != null)
        {
            var evenNode = oddNode.next;
            oddNode.next = oddNode.next.next;
            evenNode.next = oddNode.next.next;
            oddNode = oddNode.next;
        }

        oddNode.next = evenHead;
        
        return head;
    }
}

 

 

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

[LeetCode] 1448. Count Good Nodes in Binary Tree (C#)  (0) 2024.05.17
[LeetCode] 2095. Delete the Middle Node of a Linked List (C#)  (5) 2024.05.12
[LeetCode] 238. Product of Array Except Self (C#)  (0) 2024.05.12
    'Algorithm/LeetCode' 카테고리의 다른 글
    • [LeetCode] 1448. Count Good Nodes in Binary Tree (C#)
    • [LeetCode] 2095. Delete the Middle Node of a Linked List (C#)
    • [LeetCode] 238. Product of Array Except Self (C#)

    티스토리툴바