commit 5d7859c2eaf3acda006d2625fa521f4e465a4d7f parent 2a0bc686a8f5e5584637050f175832a7215d8c5f Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Sat, 22 Apr 2023 12:28:46 -0500 Palindrome linked list checker Diffstat:
| A | palindrome-linked-list/palindrome-linked-list.dart | | | 45 | +++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 45 insertions(+), 0 deletions(-)
diff --git a/palindrome-linked-list/palindrome-linked-list.dart b/palindrome-linked-list/palindrome-linked-list.dart @@ -0,0 +1,45 @@ +//Find if the values contained within a linked list are a palindrome. +//To do this I iterated through the list putting all of the values into +//an array and then at the end I checked to make sure the second half of the +//array was the same as the first half. +//The time complexity of this is O(n) where n is the number of nodes in the list. + +//Runtime: 339ms Beats: 68.25% +//Memory: 208.8MB Beats: 17.46% + + +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode? next; + * ListNode([this.val = 0, this.next]); + * } + */ +class Solution { + bool isPalindrome(ListNode? head) { + if(head?.next == null){ + return true; + } + + List<int> nums = []; + while(true){ + if(head == null){ + break; + } + nums.add(head?.val ?? 0); + head = head?.next; + } + + for(int i = 0 ; i < (nums.length / 2).ceil() ; ++i){ + if(nums[i] == nums[nums.length - 1 - i]){ + continue; + } + else{ + return false; + } + } + + return true; + } +}