LinkedList with Swift

이원희·2020년 11월 30일
0

🧰 DataStructure

목록 보기
4/9
post-thumbnail

Single LinkedList

코드

코드에 대한 설명은 여기를 참고해주세요.

class MyLinkedList {
    class LinkedListNode {
        let value: Int
        var next: LinkedListNode?
        init(value: Int, next: LinkedListNode? = nil) {
            self.value = value
            self.next = next
        }
    }
    
    var head: LinkedListNode?
    
    /** Initialize your data structure here. */
    
    init() {
        head = nil
    }
    
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    
    func get(_ index: Int) -> Int {
        guard let node = node(at: index) else {
            return -1
        }
        return node.value
    }
    
    private func node(at: Int) -> LinkedListNode? {
        guard at > -1, var node = head else {
            return nil
        }
        guard at > 0 else {
            return head
        }
        for _ in 1...at {
            guard let nextNode = node.next else {
                return nil
            }
            node = nextNode
        }
        return node
    }
    
    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    
    func addAtHead(_ val: Int) {
        let newNode = LinkedListNode(value: val, next: head)
        head = newNode
    }
    
    /** Append a node of value val to the last element of the linked list. */
    
    func addAtTail(_ val: Int) {
        guard head != nil else {
            head = LinkedListNode(value: val)
            return
        }
        var node = head
        while node?.next != nil {
            node = node?.next
        }
        node?.next = LinkedListNode(value: val)
    }
    
    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    
    func addAtIndex(_ index: Int, _ val: Int) {
        if index <= 0 {
            addAtHead(val)
            return
        }
        guard let prevNode = node(at: index - 1) else {
            return
        }
        let newNode = LinkedListNode(value: val, next: prevNode.next)
        prevNode.next = newNode
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    
    func deleteAtIndex(_ index: Int) {
        if index == 0, head != nil {
            head = head?.next
            return
        }
        guard let prevNode = node(at: index - 1) else {
            return
        }
        prevNode.next = prevNode.next?.next
    }
}

0개의 댓글