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

Commit 0b82ca4

Browse files
feat: add swift implementation to lcof2 problem: No.027 (#3026)
1 parent beb4b47 commit 0b82ca4

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

‎lcof2/剑指 Offer II 027. 回文链表/README.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,55 @@ public class Solution {
349349
}
350350
```
351351

352+
#### Swift
353+
354+
```swift
355+
/**
356+
* Definition for singly-linked list.
357+
* public class ListNode {
358+
* var val: Int
359+
* var next: ListNode?
360+
* init() { self.val = 0; self.next = nil; }
361+
* init(_ val: Int) { self.val = val; self.next = nil; }
362+
* init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
363+
* }
364+
*/
365+
366+
class Solution {
367+
func isPalindrome(_ head: ListNode?) -> Bool {
368+
guard let head = head else { return true }
369+
370+
var slow = head
371+
var fast = head.next
372+
while fast != nil && fast?.next != nil {
373+
slow = slow.next!
374+
fast = fast?.next?.next
375+
}
376+
377+
var cur = slow.next
378+
379+
var prev: ListNode? = nil
380+
while cur != nil {
381+
let nextTemp = cur?.next
382+
cur?.next = prev
383+
prev = cur
384+
cur = nextTemp
385+
}
386+
387+
var left = head
388+
var right = prev
389+
while right != nil {
390+
if left.val != right?.val {
391+
return false
392+
}
393+
left = left.next!
394+
right = right?.next
395+
}
396+
return true
397+
}
398+
}
399+
```
400+
352401
<!-- tabs:end -->
353402

354403
<!-- solution:end -->
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* var val: Int
5+
* var next: ListNode?
6+
* init() { self.val = 0; self.next = nil; }
7+
* init(_ val: Int) { self.val = val; self.next = nil; }
8+
* init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
9+
* }
10+
*/
11+
12+
class Solution {
13+
func isPalindrome(_ head: ListNode?) -> Bool {
14+
guard let head = head else { return true }
15+
16+
var slow = head
17+
var fast = head.next
18+
while fast != nil && fast?.next != nil {
19+
slow = slow.next!
20+
fast = fast?.next?.next
21+
}
22+
23+
var cur = slow.next
24+
25+
var prev: ListNode? = nil
26+
while cur != nil {
27+
let nextTemp = cur?.next
28+
cur?.next = prev
29+
prev = cur
30+
cur = nextTemp
31+
}
32+
33+
var left = head
34+
var right = prev
35+
while right != nil {
36+
if left.val != right?.val {
37+
return false
38+
}
39+
left = left.next!
40+
right = right?.next
41+
}
42+
return true
43+
}
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /