Skip to content

Instantly share code, notes, and snippets.

@Ram-1234
Created January 3, 2021 06:37
Show Gist options
  • Save Ram-1234/cd657c587e4ee2db96570506748a4fd7 to your computer and use it in GitHub Desktop.
Save Ram-1234/cd657c587e4ee2db96570506748a4fd7 to your computer and use it in GitHub Desktop.
InterviewBit LinkedList Polindrome Code In Java
specification-
Given a singly linked list, determine if its a palindrome. Return 1 or 0 denoting if its a palindrome or not, respectively.
Note:-
1-Expected solution is linear in time and constant in space.
Examples:-
List 1-->2-->1 is a palindrome.
List 1-->2-->3 is not a palindrome.
#################Code In Java################
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
public int lPalin(ListNode A) {
List<Integer> list=new ArrayList();
ListNode curr=A;
while(curr!=null){
list.add(curr.val);
curr=curr.next;
}
int i=list.size();
int count =0;
ListNode node=A;
while((i-1)>=0){
if(list.get(i-1)==node.val){
node=node.next;
count++;
}
i--;
}
if(count==list.size())
return 1;
else
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment