单链表字符串判断回文

思路

  • 使用快慢两个指针找到链表中点,慢指针每次前进一步,快指针每次前进两步
  • 在慢指针前进的过程中,同时修改其 next 指针,使得链表前半部分反序。
  • 最后比较中点两侧的链表是否相等

c版本代码见

https://github.com/hkui/algo_practice/tree/master/c/linklist/palindrome_str

java版本
https://github.com/andavid/leetcode-java/blob/master/note/234/README.md

原文地址:https://www.cnblogs.com/HKUI/p/10660304.html

时间: 2024-08-05 18:47:16

单链表字符串判断回文的相关文章

字符串-判断回文

编程判断一个字符串是否是回文,当字符串是回文时,输出字符串:yes!,否则输出字符串:no!.所谓回文即正向与反的拼向写都一样,如adgda. 长度在100以内,且全为小写字母样例输入:adgda样例输出:yes! 代码如下: 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 int main() 6 { 7 char s[100]; 8 cin >> s; 9 int n = s

判断回文链表

我们之前有两篇文章写了回文串和回文序列相关的问题. 寻找回文串的核心思想是从中心向两端扩展: string palindrome(string& s, int l, int r) { // 防止索引越界 while (l >= 0 && r < s.size() && s[l] == s[r]) { // 向两边展开 l--; r++; } // 返回以 s[l] 和 s[r] 为中心的最长回文串 return s.substr(l + 1, r - l

Java 判断回文字符串有多少和其中的最大字符串

一.简介代码功能 该代码的功能可以实现对任意的一段字符串进行判断是否有回文,回文有哪些,和其中的最大回文. 二.代码部分 1.全局变量 1 static String hws = ""; 2 static int num = 0; 3 static String[] hw; 2.创建数组用于保存回文 1 /** 2 * 创建数组保存所有的回文 3 * 4 * @return 返回一个String类型的数组 5 */ 6 public static String[] createHw()

golang 递归判断回文字符串

判断回文字符串是个比较经典的问题. 思路就是拿第一个字符和最一个字符比较,如果不等退出,相同的话继续刚刚的过程,直到第一个字符和最后一个字符相遇或者他们的距离为1时.说明他们是回文字符串. 下面的代码会忽略空白字符 如"1   1  2 1"会让为是回文字符串. golang package main import (     "fmt"     "os"     "strings"     "unicode/utf

【c语言】判断一个字符串是不是回文字符串

//判断一个字符串是不是回文字符串 #include <stdio.h> #include <assert.h> int panduan( char *p ) { char *q ; assert( *p != NULL ); q = p; while( *p != '\0') { p++; } p--; while(*q != '\0') { if( *p == *q) { p--; q++; } else return -1; } return 1; } int main()

Power oj/2610[判断回文串]

题目链接[https://www.oj.swust.edu.cn/problem/show/2610] 题意:给你一个字符串,让你判断这个字符串是不是回文串,字符串的长度是1<len<1e7,内存是4096KB. 题解:首先这1e7个字符是存不下的,1e71024=9765KB>4096kB.那么怎么办?字符串哈希,先对字符串的前半部分进行哈希,然后在对字符串后半部分进行哈希,如果两部分的哈希值相同,那么这个字符串就是回文串. BKDRH哈希,哈希公式为has=has*seed+s[i]

UVa 401 Palindromes(字符串,回文)

 Palindromes  A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string i

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

(LeetCode)Palindrome Number -- 判断回文数

Determine whether an integer is a palindrome. Do this without extra space. 解题分析: 题目很简单,但是对于要求需要看清楚. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ex