https://leetcode-cn.com/problems/odd-even-linked-list
Swift
func oddEvenList(_ head: ListNode?) -> ListNode? {
guard let h = head else {
return head
}
let aHead: ListNode? = h
var ac: ListNode? = h
let bHead: ListNode? = h.next
var bc: ListNode? = h.next
var index = 3
var current: ListNode? = bHead?.next
while current != nil {
let v = current!.val
let node = ListNode(v)
if index % 2 == 1 {
ac?.next = node
ac = ac?.next
} else {
bc?.next = node
bc = bc?.next
}
current = current?.next
index += 1
}
bc?.next = nil
ac?.next = bHead
return aHead
}
func oddEvenList1(_ head: ListNode?) -> ListNode? {
guard let h = head else {
return head
}
let aHead: ListNode? = ListNode(110)
let bHead: ListNode? = ListNode(120)
var current: ListNode? = h
var ca: ListNode? = aHead
var cb: ListNode? = bHead
var index = 1
while current != nil {
let v = current!.val
let node = ListNode(v)
if index % 2 == 1 {
ca?.next = node
ca = ca?.next
} else {
cb?.next = node
cb = cb?.next
}
current = current?.next
index += 1
}
ca?.next = bHead?.next
return aHead?.next
}