Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 755 Bytes

File metadata and controls

22 lines (17 loc) · 755 Bytes

1.3.40 Move-to-front. Read in a sequence of characters from standard input and maintain the characters in a linked list with no duplicates. When you read in a previ- ously unseen character, insert it at the front of the list. When you read in a duplicate character, delete it from the list and reinsert it at the beginning.

func moveToFront(_ input: String) -> String {
    let linkedList = LinkedList<Character>()

    for character in input {
        for node in linkedList {
            if node.value == character {
                node.next?.prev = node.prev
                node.prev?.next = node.next
                break;
            }
        }

        linkedList.appendHead(character)
    }


    return linkedList.description
}