Skip to content

Commit 26cf7ae

Browse files
refactor 234
1 parent 4804888 commit 26cf7ae

File tree

1 file changed

+20
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-0
lines changed

src/main/java/com/fishercoder/solutions/_234.java

+20
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,24 @@ public boolean isPalindrome(ListNode head) {
8080
}
8181
}
8282

83+
public static class Solution3 {
84+
/**
85+
* O(n) time
86+
* O(n) space
87+
*/
88+
public boolean isPalindrome(ListNode head) {
89+
List<Integer> list = new ArrayList<>();
90+
while (head != null) {
91+
list.add(head.val);
92+
head = head.next;
93+
}
94+
for (int i = 0, j = list.size() - 1; i <= j; i++, j--) {
95+
if (list.get(i) != list.get(j)) {
96+
return false;
97+
}
98+
}
99+
return true;
100+
}
101+
}
102+
83103
}

0 commit comments

Comments
 (0)