Skip to content

Commit 7068f72

Browse files
committed
solution for: palindrome linked list
1 parent c63a3d6 commit 7068f72

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode;
2+
3+
import java.util.Stack;
4+
5+
public class PalindromeLinkedList {
6+
7+
public static void main(String[] args){
8+
ListNode r = new ListNode(2);
9+
r.next= new ListNode(1);
10+
r.next.next = new ListNode(2);
11+
PalindromeLinkedList test = new PalindromeLinkedList();
12+
System.out.println(test.isPalindrome(r));
13+
}
14+
15+
public boolean isPalindrome(ListNode head) {
16+
if(head == null) return true;
17+
ListNode node = head;
18+
Stack<Integer> s = new Stack<>();
19+
while(node!=null){
20+
s.push(node.val);
21+
node = node.next;
22+
}
23+
while(head!=null){
24+
if(head.val!= s.pop())
25+
return false;
26+
head = head.next;
27+
}
28+
return true;
29+
}
30+
}

0 commit comments

Comments
 (0)