이중 연결 문자 목록이 회문인지 아닌지 확인

이중 연결 문자 목록이 회문인지 아닌지 확인

주어진 이중 연결 리스트 ~의 문자 이중 연결리스트인지 확인하는 작업입니다. 회문 아니면.

예:

입력:

이중으로 연결된 문자 목록이 회문인지 아닌지 확인


산출: 진실
설명: 목록은 회문인 'LEVEL'에 해당합니다.


입력:

이중으로 연결된 문자 목록이 회문인지 아닌지 확인 -2


산출: 거짓
설명: 목록은 회문이 아닌 'LEVES'에 해당합니다.

접근하다:

아이디어는 두 개의 포인터를 초기화하는 것입니다. 왼쪽 (처음에는 머리로 설정됨) 및 오른쪽 (처음에는 tail로 설정됨) 두 포인터의 값을 비교하는 동안 왼쪽 null과 같지 않거나 왼쪽 다음으로 이동했습니다 오른쪽. 두 포인터의 값이 다음과 같은 경우 동일한 이동하다 왼쪽 다음 포인터로 오른쪽 이전 포인터로. 그렇지 않으면 false를 반환합니다.

다음은 위의 접근 방식을 구현한 것입니다.

C++
   // C++ program to check if a doubly    // linked list is palindrome.   #include          using     namespace     std  ;   class     Node     {   public  :      char     data  ;      Node  *     prev       *  next  ;      Node     (  char     x  )     {      data     =     x  ;      prev     =     nullptr  ;      next     =     nullptr  ;      }   };   // Function that returns true if the   // doubly linked list is a palindrome   bool     isPalindrome  (  Node  *     head  )     {      if     (  head     ==     nullptr  )     return     true  ;          // Find the tail ptr.      Node     *  left  =  head       *  right  =  head  ;      while     (  right  ->  next     !=     nullptr  )     {      right     =     right  ->  next  ;      }          // Check if the doubly linked list is       // a palindrome.      while     (  left  !=  right     &&     left  ->  prev  !=  right  )     {          // If char mismatch return      // false.      if     (  left  ->  data     !=     right  ->  data  )      return     false  ;          // Move the pointers      left     =     left  ->  next  ;      right     =     right  ->  prev  ;      }          return     true  ;   }   int     main  ()     {          // Doubly Linked list:       // L  <-> E  <-> V  <-> E  <-> L      Node     *  head     =     new     Node  (  'L'  );      head  ->  next     =     new     Node  (  'E'  );      head  ->  next  ->  prev     =     head  ;      head  ->  next  ->  next     =     new     Node  (  'V'  );      head  ->  next  ->  next  ->  prev     =     head  ->  next  ;      head  ->  next  ->  next  ->  next     =     new     Node  (  'E'  );      head  ->  next  ->  next  ->  next  ->  prev     =     head  ->  next  ->  next  ;      head  ->  next  ->  next  ->  next  ->  next     =     new     Node  (  'L'  );      head  ->  next  ->  next  ->  next  ->  next  ->  prev     =     head  ->  next  ->  next  ->  next  ;      if     (  isPalindrome  (  head  ))      cout      < <     'True'  ;      else      cout      < <     'False'  ;      return     0  ;   }   
C
   // C program to check if a doubly    // linked list is palindrome.   #include         #include         struct     Node     {      char     data  ;      struct     Node  *     prev  ;      struct     Node  *     next  ;   };   // Function that returns true if the   // doubly linked list is a palindrome   int     isPalindrome  (  struct     Node  *     head  )     {      if     (  head     ==     NULL  )     return     1  ;          // Find the tail ptr.      struct     Node     *  left     =     head       *  right     =     head  ;      while     (  right  ->  next     !=     NULL  )     {      right     =     right  ->  next  ;      }          // Check if the doubly linked list is       // a palindrome.      while     (  left     !=     right     &&     left  ->  prev     !=     right  )     {          // If char mismatch return      // false.      if     (  left  ->  data     !=     right  ->  data  )      return     0  ;          // Move the pointers      left     =     left  ->  next  ;      right     =     right  ->  prev  ;      }          return     1  ;   }   struct     Node  *     createNode  (  char     x  )     {      struct     Node  *     newNode     =         (  struct     Node  *  )  malloc  (  sizeof  (  struct     Node  ));      newNode  ->  data     =     x  ;      newNode  ->  prev     =     NULL  ;      newNode  ->  next     =     NULL  ;      return     newNode  ;   }   int     main  ()     {          // Doubly Linked list:       // L  <-> E  <-> V  <-> E  <-> L      struct     Node     *  head     =     createNode  (  'L'  );      head  ->  next     =     createNode  (  'E'  );      head  ->  next  ->  prev     =     head  ;      head  ->  next  ->  next     =     createNode  (  'V'  );      head  ->  next  ->  next  ->  prev     =     head  ->  next  ;      head  ->  next  ->  next  ->  next     =     createNode  (  'E'  );      head  ->  next  ->  next  ->  next  ->  prev     =     head  ->  next  ->  next  ;      head  ->  next  ->  next  ->  next  ->  next     =     createNode  (  'L'  );      head  ->  next  ->  next  ->  next  ->  next  ->  prev     =     head  ->  next  ->  next  ->  next  ;      if     (  isPalindrome  (  head  ))      printf  (  'True  n  '  );      else      printf  (  'False  n  '  );      return     0  ;   }   
Java
   // Java program to check if a doubly    // linked list is palindrome.   class   Node     {      char     data  ;      Node     prev       next  ;      Node  (  char     x  )     {      data     =     x  ;      prev     =     null  ;      next     =     null  ;      }   }   class   GfG     {      // Function that returns true if the      // doubly linked list is a palindrome      static     boolean     isPalindrome  (  Node     head  )     {      if     (  head     ==     null  )     return     true  ;          // Find the tail ptr.      Node     left     =     head       right     =     head  ;      while     (  right  .  next     !=     null  )     {      right     =     right  .  next  ;      }          // Check if the doubly linked list is       // a palindrome.      while     (  left     !=     right     &&     left  .  prev     !=     right  )     {          // If char mismatch return      // false.      if     (  left  .  data     !=     right  .  data  )      return     false  ;          // Move the pointers      left     =     left  .  next  ;      right     =     right  .  prev  ;      }          return     true  ;      }      public     static     void     main  (  String  []     args  )     {      // Doubly Linked list:       // L  <-> E  <-> V  <-> E  <-> L      Node     head     =     new     Node  (  'L'  );      head  .  next     =     new     Node  (  'E'  );      head  .  next  .  prev     =     head  ;      head  .  next  .  next     =     new     Node  (  'V'  );      head  .  next  .  next  .  prev     =     head  .  next  ;      head  .  next  .  next  .  next     =     new     Node  (  'E'  );      head  .  next  .  next  .  next  .  prev     =     head  .  next  .  next  ;      head  .  next  .  next  .  next  .  next     =     new     Node  (  'L'  );      head  .  next  .  next  .  next  .  next  .  prev     =     head  .  next  .  next  .  next  ;      if     (  isPalindrome  (  head  ))      System  .  out  .  println  (  'True'  );      else      System  .  out  .  println  (  'False'  );      }   }   
Python
   # Python program to check if a doubly    # linked list is palindrome.   class   Node  :   def   __init__  (  self     x  ):   self  .  data   =   x   self  .  prev   =   None   self  .  next   =   None   # Function that returns true if the   # doubly linked list is a palindrome   def   isPalindrome  (  head  ):   if   head   is   None  :   return   True   # Find the tail ptr.   left   =   head   right   =   head   while   right  .  next   is   not   None  :   right   =   right  .  next   # Check if the doubly linked list is    # a palindrome.   while   left   !=   right   and   left  .  prev   !=   right  :   # If char mismatch return   # false.   if   left  .  data   !=   right  .  data  :   return   False   # Move the pointers   left   =   left  .  next   right   =   right  .  prev   return   True   if   __name__   ==   '__main__'  :   # Doubly Linked list:    # L  <-> E  <-> V  <-> E  <-> L   head   =   Node  (  'L'  )   head  .  next   =   Node  (  'E'  )   head  .  next  .  prev   =   head   head  .  next  .  next   =   Node  (  'V'  )   head  .  next  .  next  .  prev   =   head  .  next   head  .  next  .  next  .  next   =   Node  (  'E'  )   head  .  next  .  next  .  next  .  prev   =   head  .  next  .  next   head  .  next  .  next  .  next  .  next   =   Node  (  'L'  )   head  .  next  .  next  .  next  .  next  .  prev   =   head  .  next  .  next  .  next   if   isPalindrome  (  head  ):   print  (  'True'  )   else  :   print  (  'False'  )   
C#
   // C# program to check if a doubly    // linked list is palindrome.   using     System  ;   class     Node     {      public     char     data  ;      public     Node     prev       next  ;      public     Node  (  char     x  )     {      data     =     x  ;      prev     =     null  ;      next     =     null  ;      }   }   class     GfG     {      // Function that returns true if the      // doubly linked list is a palindrome      static     bool     isPalindrome  (  Node     head  )     {      if     (  head     ==     null  )     return     true  ;          // Find the tail ptr.      Node     left     =     head       right     =     head  ;      while     (  right  .  next     !=     null  )     {      right     =     right  .  next  ;      }          // Check if the doubly linked list is       // a palindrome.      while     (  left     !=     right     &&     left  .  prev     !=     right  )     {          // If char mismatch return      // false.      if     (  left  .  data     !=     right  .  data  )      return     false  ;          // Move the pointers      left     =     left  .  next  ;      right     =     right  .  prev  ;      }          return     true  ;      }      static     void     Main  (  string  []     args  )     {      // Doubly Linked list:       // L  <-> E  <-> V  <-> E  <-> L      Node     head     =     new     Node  (  'L'  );      head  .  next     =     new     Node  (  'E'  );      head  .  next  .  prev     =     head  ;      head  .  next  .  next     =     new     Node  (  'V'  );      head  .  next  .  next  .  prev     =     head  .  next  ;      head  .  next  .  next  .  next     =     new     Node  (  'E'  );      head  .  next  .  next  .  next  .  prev     =     head  .  next  .  next  ;      head  .  next  .  next  .  next  .  next     =     new     Node  (  'L'  );      head  .  next  .  next  .  next  .  next  .  prev     =     head  .  next  .  next  .  next  ;          if     (  isPalindrome  (  head  ))      Console  .  WriteLine  (  'True'  );      else      Console  .  WriteLine  (  'False'  );      }   }   
JavaScript
   // JavaScript program to check if a doubly    // linked list is palindrome.   class     Node     {      constructor  (  x  )     {      this  .  data     =     x  ;      this  .  prev     =     null  ;      this  .  next     =     null  ;      }   }   // Function that returns true if the   // doubly linked list is a palindrome   function     isPalindrome  (  head  )     {      if     (  head     ===     null  )     return     true  ;          // Find the tail ptr.      let     left     =     head       right     =     head  ;      while     (  right  .  next     !==     null  )     {      right     =     right  .  next  ;      }          // Check if the doubly linked list is       // a palindrome.      while     (  left     !==     right     &&     left  .  prev     !==     right  )     {          // If char mismatch return      // false.      if     (  left  .  data     !==     right  .  data  )      return     false  ;          // Move the pointers      left     =     left  .  next  ;      right     =     right  .  prev  ;      }          return     true  ;   }   // Doubly Linked list:    // L  <-> E  <-> V  <-> E  <-> L   let     head     =     new     Node  (  'L'  );   head  .  next     =     new     Node  (  'E'  );   head  .  next  .  prev     =     head  ;   head  .  next  .  next     =     new     Node  (  'V'  );   head  .  next  .  next  .  prev     =     head  .  next  ;   head  .  next  .  next  .  next     =     new     Node  (  'E'  );   head  .  next  .  next  .  next  .  prev     =     head  .  next  .  next  ;   head  .  next  .  next  .  next  .  next     =     new     Node  (  'L'  );   head  .  next  .  next  .  next  .  next  .  prev     =     head  .  next  .  next  .  next  ;   if     (  isPalindrome  (  head  ))      console  .  log  (  'True'  );   else      console  .  log  (  'False'  );   

산출
True 

시간 복잡도: O(n) 여기서 n은 이중 연결 리스트의 노드 수입니다.
보조 공간: 오(1)

관련 기사:   

  • 단일 연결 리스트가 회문인지 확인하는 함수
  • 문자열의 연결 목록이 회문을 형성하는지 확인
퀴즈 만들기