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!

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

Algorithm/LeetCode

[LeetCode] 2095. Delete the Middle Node of a Linked List (C#)

2024. 5. 12. 18:16

https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list

 

linkedlist의 head가 주어진다.

해당 linkedlist의 middle 노드를 삭제하고, 변경된 리스트의 head를 반환하는 문제.

middle 노드의 기준은 리스트의 사이즈를 n이라고 가정하고 [ n / 2 ] 번째 노드를 뜻한다.

 

 

1. 원 포인터를 사용한 풀이

  • middle 노드가 몇 번째인지 확인하는 과정이 들어간다.
  • middle 노드 직전에서 순회를 멈추고 다다음 노드를 Next 로 바라본다.
  • 원소가 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 class Solution
{
    public ListNode DeleteMiddle(ListNode head)
    {
        if (head.next == null)
        {
            return null;
        }
        
        var current = head;
        var listLength = 0;
        while (current != null)
        {
            listLength++;
            current = current.next;
        }

        var togo = listLength / 2;
        current = head;
        while (togo-- != 1)
        {
            current = current.next;
        }
        
        current.next = current.next.next;
        
        return head;
    }
}

 

 

 

2. 투포인터를 사용한 풀이

  • 하나씩 이동하는 slow 포인터와 2칸씩 이동하는 fast 포인터를 함께 사용한다.
  • fast 포인터가 더 이상 움직이지 못하는 시점이 slow 포인터의 middle 직전 노드가 된다.
public class Solution
{
    public ListNode DeleteMiddle(ListNode head)
    {
        var slowPrev = new ListNode(-1);
        slowPrev.next = head;
        
        var slow = slowPrev;
        var fast = head;

        while (fast?.next != null)
        {
            slow = slow.next;
            fast = fast.next.next;
        }

        slow.next = slow.next.next;

        return slowPrev.next;
    }
}

 

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

[LeetCode] 1448. Count Good Nodes in Binary Tree (C#)  (0) 2024.05.17
[LeetCode] 328. Odd Even Linked List (C#)  (0) 2024.05.17
[LeetCode] 238. Product of Array Except Self (C#)  (0) 2024.05.12
    'Algorithm/LeetCode' 카테고리의 다른 글
    • [LeetCode] 1448. Count Good Nodes in Binary Tree (C#)
    • [LeetCode] 328. Odd Even Linked List (C#)
    • [LeetCode] 238. Product of Array Except Self (C#)

    티스토리툴바