Remove Nth Node From End of List

2021. 7. 3. 16:26· 자료구조 알고리즘/코딩테스트

19. Remove Nth Node From End of List

난이도: Easy

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

 

Remove Nth Node From End of List - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

Language: C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* node = head;
        int length = 0;
        while (node) {
            length++;
            node = node->next;
        }
        
        length = length - n;
        if (length < 0)
            return NULL;
        else {
            node = head;
            while (length > 1) {
                node = node->next;
                length--;
            }
            if (length == 0)
                head = head->next;
            else
                node->next = node->next->next;
            
        }
        
        return head;
        
    }
};

 

야호

아마도 지금 푼 것 중에서 처음으로 내가 혼자 다 풀었다.. (난이도 Easy...)

솔루션은 dummy라고 dummy.next = head 를 넣어서 while (length > 0) 하고, node->next = node->next->next; 만 쓰도록 하여 간소화 시켰다.

728x90
반응형

'자료구조 알고리즘 > 코딩테스트' 카테고리의 다른 글

Remove Linked List Elements  (0) 2021.07.06
Reverse Linked List  (0) 2021.07.04
Intersection of Two Linked Lists  (0) 2021.07.03
Linked List Cycle II  (0) 2021.07.01
Linked List Cycle  (0) 2021.06.29
'자료구조 알고리즘/코딩테스트' 카테고리의 다른 글
  • Remove Linked List Elements
  • Reverse Linked List
  • Intersection of Two Linked Lists
  • Linked List Cycle II
내공얌냠
내공얌냠
내공냠냠
내공얌냠
내공냠냠
내공얌냠
전체
오늘
어제
  • 분류 전체보기 (257) N
    • 개발 (91) N
      • mediapipe (16)
      • insightface (5)
      • JongjuAR (3)
    • 자료구조 알고리즘 (79)
      • 코딩테스트 (64)
      • 이론 (15)
    • 공부 (54)
      • 단행본 (8)
      • 튜토리얼 (19)
      • 논문 (15)
      • 복기 (5)
    • 참여 (5)

블로그 메뉴

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

공지사항

인기 글

태그

  • python telegrambot
  • mediapipe
  • 플러터
  • claude cli 사용기
  • 컴퓨터 비전
  • 머신러닝이란
  • 구글 미디어파이프
  • flutter 행사 후기
  • 딥러닝 기반 음성인식 기초
  • 음성인식 기초
  • 음성인식 튜토리얼
  • 깃 튜토리얼
  • flutter 행사
  • ios google places api
  • flutter
  • 플러터 튜토리얼
  • 컴퓨터 비전 책 추천
  • 미디어파이프
  • google mediapipe
  • mediapipe translate
  • postgresql 재설치
  • 컴퓨터 비전 기초
  • claude cli 사용방법
  • postgresql install in mac
  • flutter tutorial
  • claude cli 설치
  • flutter conference
  • vscode 스프링 설치
  • speaker adaptation tts
  • git tutorial

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.2
내공얌냠
Remove Nth Node From End of List
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.