
g0201_0300.s0234_palindrome_linked_list.Solution.swift Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Easy #Top_100_Liked_Questions #Two_Pointers #Stack #Linked_List #Recursion
// #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
// #2024_07_01_Time_290_ms_(100.00%)_Space_22.1_MB_(36.73%)
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func isPalindrome(_ head: ListNode?) -> Bool {
var arr = [Int]()
var head = head
while let node = head {
arr.append(node.val)
head = node.next
}
var i = 0
var j = arr.count-1
while i < j {
if arr[i] != arr[j] {
return false
}
i += 1
j -= 1
}
return true
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy