Add to List 234. Palindrome Linked List


  • 0
    R

    First we iterate through the Linkedlist, get all the elements into a list . then we have the count and we iterate again till half of the list by index and keep a check weather 0th equals nth , then 1st equals n-1th element
    '''
    public bool IsPalindrome(ListNode head) {
    List<int> lstItems = new List<int>();
    ListNode current = head;
    while(current!=null){
    lstItems.Add(current.val);
    current = current.next;
    }
    if(lstItems.Count<=1)
    {
    return true;

        }else{
            int lstCount = lstItems.Count;
            for(int i = 0; i <= (lstCount - 1) / 2; i++){
                if(!(lstItems[i]==lstItems[lstCount-i-1])){
                    return false;
                }                
            }
            return true;
        }
    }
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.