베어_
TechBear
베어_
전체 방문자
오늘
어제
  • 분류 전체보기 (336)
    • Spring (33)
      • 개념 (13)
      • Security (5)
      • 실습 (1)
      • 토비 스프링 (11)
    • JPA (6)
    • 프로젝트 기록 (24)
    • DB (13)
    • JAVA (18)
    • 알고리즘 (50)
      • 유형정리 (8)
      • Baekjoon (21)
      • LeetCode (18)
    • 디자인패턴 (0)
    • 개발서적 (79)
      • Effective Java (78)
      • 객체지향의 사실과 오해 (1)
    • 독후감 (4)
    • 보안 (2)
    • 운영체제(OS) (53)
      • 공룡책 (53)
    • 컴퓨터 네트워크 (28)
      • 컴퓨터 네트워크 하향식 접근 (23)
    • 자료구조 (1)
    • DevOps (2)
    • 앱 개발 (20)
      • 안드로이드 스튜디오 (20)

블로그 메뉴

    공지사항

    인기 글

    태그

    • 스프링시큐리티
    • 운영체제
    • 데이터베이스
    • leetcode
    • 함수형인터페이스
    • 이펙티브자바
    • java
    • 알고리즘
    • Spring
    • 스프링
    • C++
    • 토비스프링
    • BFS
    • dfs
    • 자바
    • 자바8
    • jpa
    • 백준
    • 코드업
    • 스레드

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    알고리즘/LeetCode

    [LeetCode] Remove Nth Node From End of List (Java)

    2023. 7. 29. 17:38

    ✉️문제

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/

     

    Remove Nth Node From End of List - LeetCode

    Can you solve this real interview question? Remove Nth Node From End of List - Given the head of a linked list, remove the nth node from the end of the list and return its head.   Example 1: [https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg]

    leetcode.com

     

    📝 접근

    O(n)에 푸는 것이 핵심이다. 이를 위해 투 포인터를 이용한다. 투 포인터의 간격은 n으로 설정한다.

     

    🗝 문제풀이

    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            if(head.next == null) return null;
            ListNode first = head;
            ListNode second = head;
    
            for(int i = 0; i < n; i++) {
                second = second.next;
            }
    
            if(second == null) return first.next;
    
            while(second.next != null) {
                first = first.next;
                second = second.next;
            }
        
    
            first.next = first.next.next;
            return head;
        }
    }
    저작자표시 비영리 변경금지 (새창열림)
      '알고리즘/LeetCode' 카테고리의 다른 글
      • [LeetCode] Merge k Sorted Lists (Java)
      • [LeetCode] Longest Consecutive Sequence (Java)
      • [LeetCode] Longest Consecutive Sequence (Java)
      • [LeetCode] Course Schedule (Java)
      베어_
      베어_
      Today I learned | 문제를 해결하는 개발자

      티스토리툴바