-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55139b7
commit 7071f83
Showing
3 changed files
with
71 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,49 @@ | ||
// | ||
// Created by draco on 9/12/23. | ||
// | ||
#pragma once | ||
|
||
#ifndef LEETCODEJOURNEY_SOLUTION_HPP | ||
#define LEETCODEJOURNEY_SOLUTION_HPP | ||
#include <ranges> | ||
#include <vector> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <gtest/gtest.h> | ||
|
||
#endif //LEETCODEJOURNEY_SOLUTION_HPP | ||
struct ListNode { | ||
int32_t value; | ||
ListNode *next; | ||
}; | ||
|
||
class Solution { | ||
public: | ||
static auto is_palindrome(ListNode *head) -> bool { | ||
std::vector<int32_t> numbers; | ||
|
||
while (head != nullptr) { | ||
numbers.emplace_back(head->value); | ||
head = head->next; | ||
} | ||
|
||
std::size_t left = 0; | ||
std::size_t right = numbers.size() - 1; | ||
|
||
while (left < right) { | ||
if (numbers[left] != numbers[right]) { | ||
return false; | ||
} ++left, --right; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static auto insert(ListNode **head_ref, int32_t new_value) -> void { | ||
auto *node = new ListNode(); | ||
node->value = new_value; | ||
node->next = (*head_ref); | ||
(*head_ref) = node; | ||
} | ||
|
||
static auto print(ListNode *head) -> void { | ||
while (head != nullptr) { | ||
std::cout << head->value << ' '; | ||
head = head->next; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
// | ||
// Created by draco on 9/12/23. | ||
// | ||
#include "../include/solution.hpp" | ||
|
||
auto main() -> int { | ||
ListNode *head = nullptr; | ||
Solution::insert(&head, 1); | ||
Solution::insert(&head, 2); | ||
Solution::insert(&head, 2); | ||
Solution::insert(&head, 1); | ||
|
||
std::cout << std::boolalpha << Solution::is_palindrome(head); | ||
|
||
return 0; | ||
} |