Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python determine whether a single linked list is a palindromic linked list?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Python how to judge whether a single linked list is a palindrome linked list", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to judge whether a single linked list is a palindrome linked list" bar!

Determine whether a single linked list is a palindromic linked list.

For example, [1, 2, 3, 2, 1] is a palindrome linked list, which looks at the elements in the linked list in turn and vice versa.

Ideas for solving the problem:

The first step that must be done is to determine whether the input list is empty or has only one element. If empty or there is only one element, the linked list returns true for the palindromic linked list.

Use the fast and slow pointer to find the midpoint of the linked list. The slow pointer takes one step at a time, and the fast pointer takes two steps at a time.

According to the midpoint of the linked list found, all the values of the second half of the linked list are reversed and compared with the values of the overall single linked list from scratch. If one value is different, it is not a palindromic linked list, otherwise it is a palindromic linked list.

If you enter a single linked list: [1, 2, 3, 2, 1]

Reverse the single linked list from the second half: [1, 2]

Language:C

/ * Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode* next; *}; * / struct ListNode* reverseList (struct ListNode* head) {struct ListNode* pre= (struct ListNode*) malloc (sizeof (struct ListNode)); struct ListNode* next= (struct ListNode*) malloc (sizeof (struct ListNode)); pre=NULL; next=NULL;while (headcount null) {next=head- > next; head- > next=pre; pre=head; head=next;} return pre } bool isPalindrome (struct ListNode* head) {struct ListNode* slow = (struct ListNode*) malloc (sizeof (struct ListNode)); struct ListNode* fast = (struct ListNode*) malloc (sizeof (struct ListNode)); if (head = = NULL | | head- > next = = NULL) {return true;} fast = head; slow = head;while (fast- > next & & fast- > next- > next) {slow = slow- > next; fast = fast- > next- > next;} slow = reverseList (slow- > slow-) While (slow) {if (head- > val! = slow- > val) {return false;} head = head- > next; slow = slow- > next;} return true;}

Language:cpp

/ * Definition for singly-linked list. * struct ListNode {* int val; * ListNode* next; * ListNode (int x): val (x), next (NULL) {} *}; * / class Solution {public: bool isPalindrome (ListNode* head) {if (head = = NULL | | head- > next = = NULL) {return true;} ListNode* slow = head; ListNode* fast = head While (fast- > next & & fast- > next- > next) {slow = slow- > next; fast = fast- > next- > next;} slow = reverseList (slow- > next); while (slow) {if (head- > val! = slow- > val) {return false;} head = head- > next; slow = slow- > next;} return true } ListNode* reverseList (ListNode* head) {ListNode* pre= NULL; ListNode* next= NULL;while (headcount null) {next=head- > next; head- > next=pre; pre=head; head=next;} return pre;}} Thank you for your reading, the above is the content of "Python how to judge whether a single linked list is a palindrome linked list". After the study of this article, I believe you have a deeper understanding of how to judge whether a single linked list is a palindrome linked list by Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report