220v
젝무의 개발새발
220v
전체 방문자
오늘
어제
  • 분류 전체보기 (255)
    • AI (35)
      • ML, DL 학습 (30)
      • 논문 리뷰 (4)
      • 실습 및 프로젝트 (1)
    • Algorithm (145)
      • LeetCode (13)
      • 프로그래머스 (35)
      • 백준 (96)
      • 알고리즘, 문법 정리 (1)
    • Mobile, Application (17)
      • Flutter (10)
      • iOS, MacOS (7)
    • BackEnd (7)
      • Flask (1)
      • Node.js (5)
      • Spring, JSP..etc (1)
    • Web - FrontEnd (18)
      • JavaScript, JQuery, HTML, C.. (12)
      • React (6)
    • DataBase (1)
      • MySQL (1)
      • Firebase Firestore (0)
      • Supabase (0)
    • Git (1)
    • 기타 툴 및 오류 해결 (3)
    • 강의 (5)
      • Database (3)
      • 암호학 (2)
      • 알고리즘 (0)
    • 후기와 회고 (2)
    • 블로그 꾸미기 (1)
    • 일상과 이것저것 (20)
      • 맛집 (12)
      • 세상사는일 (4)
      • 도서리뷰 (1)
      • 이런저런 생각들 (잡글) (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 다익스트라
  • simulation
  • BFS
  • Priority Queue
  • REACT
  • bitmasking
  • disjoint set
  • topological sort
  • dfs
  • 티스토리챌린지
  • 위상 정렬
  • top-down
  • dp
  • Greedy
  • Backtracking
  • 백준
  • Prefix Sum
  • Minimum Spanning Tree
  • IMPLEMENT
  • 구현
  • 프로그래머스
  • union-find
  • implementation
  • Dynamic Programming
  • Mathematics
  • 오블완
  • brute-Force
  • binary search
  • two pointer
  • Lis

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
220v

젝무의 개발새발

Algorithm/LeetCode

[LeetCode/릿코드] 234. Palindrome Linked List

2022. 6. 21. 22:40

[ 처음 풀이 ]

재귀함수를 이용해, input에서 주어지는 Linked List를 반대로 연결한 것을 구하고,

( 1->2->3->2->1의 Reverse라면 1<-2<-3<-2<-1)

구한 Reverse된 List와 원래 List를 Root Node부터 끝까지 항상 같은지 비교.

(중간 노드까지만 비교해도 되나, 중간 노드를 구하는 과정이 더 계산이 길어지는 것 같아 패스)

/**
 * 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 {
private:
    ListNode* tail = new ListNode();
    ListNode* startTail = tail;
    ListNode* getEnd(ListNode* head){
        ListNode* end = head;
        while(end->next != nullptr){
            end = end->next;
        }
        return end;
    }
    
    void getReverse(ListNode* head){
        ListNode* temp = getEnd(head);
        tail->val = temp->val;
        ListNode* tempHead = head;
        getReverse_(tempHead);
    }
    
    void getReverse_(ListNode* node){
        if(node->next != nullptr){
            ListNode* newNode = new ListNode(node->val);
            node = node->next;
            getReverse_(node);
            startTail->next = newNode;
            startTail = newNode;
        }
    }
    
public:
    bool isPalindrome(ListNode* head) {
        getReverse(head);
        while(head != nullptr){
            if(head->val == tail->val){
                head = head->next;
                tail = tail->next;
            } else {
                return false;
            }
        }
        return true;
    }
};
    'Algorithm/LeetCode' 카테고리의 다른 글
    • [LeetCode/릿코드] 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (220702 daily challenge)
    • [LeetCode/릿코드] 215. Kth Largest Element in an Array (220622 daily challenge)
    • [LeetCode/릿코드] 383. Ransom Note
    • [LeetCode/릿코드] 13. Roman to Integer
    220v
    220v
    DGU CSE 20 / Apple Developer Academy @ POSTECH 2nd Jr.Learner.

    티스토리툴바