GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/code/lc234.java at master · mJackie/leetcode

Expand file tree

/

lc234.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
44 lines (43 loc) · 1.25 KB
/

lc234.java

Copy path

File metadata and controls

44 lines (43 loc) · 1.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package code;
/*
* 234. Palindrome Linked List
* 题意:判断链表是否是回文的
* 难度:Easy
* 分类:LinkedList, Two Pointers
* 思路:反转一半就行了,避免了空间开销
* Tips:很好的题,考了 Two Pointers, 还考了链表反转
* lc5, lc9, lc125, lc131, lc234, lc647
*/
public class lc234 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public boolean isPalindrome(ListNode head) {
if(head==null||head.next==null)
return true;
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode head2 = reverse(slow.next,null);
slow.next = null; //注意截断一下
while(head2!=null&&head!=null){
if(head2.val!=head.val)
return false;
head = head.next;
head2 = head2.next;
}
return true;
}
public ListNode reverse(ListNode head, ListNode pre){
ListNode next = head.next;
head.next = pre;
if(next==null)
return head;
return reverse(next, head);
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.