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

Appearance settings

Latest commit

 

History

History
History
37 lines (33 loc) · 1.21 KB

File metadata and controls

37 lines (33 loc) · 1.21 KB
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
package ch08;
import datatype.ListNode;
public class P13_3 {
public boolean isPalindrome(ListNode head) {
ListNode fast = head, slow = head;
// 빠른 러너가 끝까지 갈 때까지 느린 러너와 함께 진행
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
// 홀수 개일 때 느린 러너가 한 칸 더 앞으로 가도록 처리
if (fast != null)
slow = slow.next;
// 중간에 도달한 느린 러너를 기준으로 하여 역순 연결 리스트를 만든다
ListNode rev = null;
while (slow != null) {
// 느린 러너로 역순 연결 리스트 rev를 만들어 나간다
ListNode next = slow.next;
slow.next = rev;
rev = slow;
slow = next;
}
// rev 연결 리스트를 끝까지 이동하며 비교
while (rev != null) {
// 역순 연결 리스트 rev와 기존 연결 리스트 head를 차례대로 비교
if (rev.val != head.val)
return false;
rev = rev.next;
head = head.next;
}
return true;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.