Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
/pythonPublic archive

Commit337e300

Browse files
whoisnpabranhe
authored andcommitted
added is_palindrome
1 parentebd5e43 commit337e300

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
defis_palindrome(head):
2+
ifnothead:
3+
returnTrue
4+
fast,slow=head.next,head
5+
whilefastandfast.next:
6+
fast=fast.next.next
7+
slow=slow.next
8+
second=slow.next
9+
node=None
10+
whilesecond:
11+
nxt=second.next
12+
second.next=node
13+
node=second
14+
second=nxt
15+
whilenode:
16+
ifnode.val!=head.val:
17+
returnFalse
18+
node=node.next
19+
head=head.next
20+
returnTrue
21+
22+
23+
defis_palindrome_stack(head):
24+
ifnotheadornothead.next:
25+
returnTrue
26+
27+
# Get the midpoint
28+
slow=fast=cur=head
29+
whilefastandfast.next:
30+
fast,slow=fast.next.next,slow.next
31+
32+
# Push the second half into the stack
33+
stack= [slow.val]
34+
whileslow.next:
35+
slow=slow.next
36+
stack.append(slow.val)
37+
38+
# Comparison
39+
whilestack:
40+
ifstack.pop()!=cur.val:
41+
returnFalse
42+
cur=cur.next
43+
44+
returnTrue
45+
46+
47+
defis_palindrome_dict(head):
48+
ifnotheadornothead.next:
49+
returnTrue
50+
d= {}
51+
pos=0
52+
whilehead:
53+
ifhead.valind.keys():
54+
d[head.val].append(pos)
55+
else:
56+
d[head.val]= [pos]
57+
head=head.next
58+
pos+=1
59+
checksum=pos-1
60+
middle=0
61+
forvind.values():
62+
iflen(v)%2!=0:
63+
middle+=1
64+
else:
65+
step=0
66+
foriinrange(0,len(v)):
67+
ifv[i]+v[len(v)-1-step]!=checksum:
68+
returnFalse
69+
step+=1
70+
ifmiddle>1:
71+
returnFalse
72+
returnTrue

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp