Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Leetcode/Palindrome Linked List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {

ListNode temp = head;
Stack<Integer> st = new Stack<>();
int count=0;
while(temp!=null){
count++;
temp = temp.next;
}

System.out.println(count);

temp = head;
int i=0;
for(i=0;i<count;i++){
st.push(temp.val);
temp = temp.next;
}
temp=head;
while(temp.next != null && !st.isEmpty()){
if(st.pop() != temp.val){
return false;
}
temp = temp.next;
}



return true;

}
}