-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path61-rotate-list.swift
39 lines (38 loc) · 1.11 KB
/
61-rotate-list.swift
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
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
// Time O(n)
// Space O(1)
func rotateRight(_ head: ListNode?, _ k: Int) -> ListNode? {
var last: ListNode? = nil
let sz = size(head, &last)
guard sz > 0 else { return head }
var k = sz - (k % sz)
last?.next = head
var newTail = last, newHead = head
while k > 0 {
k -= 1
newTail = newHead
newHead = newHead?.next
}
newTail?.next = nil
return newHead
}
func size(_ head: ListNode?, _ last: inout ListNode?) -> Int {
var size = 0, curr: ListNode? = head
while curr != nil {
size += 1
if curr?.next == nil { last = curr }
curr = curr?.next
}
return size
}
}