[Data Structures] LinkedList - Removing Values

Removing Values

์ด๋ฒˆ์—” LinkedList์— ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ํ™•์ธ!

๊ฐ’ ์ œ๊ฑฐ์—๋Š” ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  • ์•ž์—์„œ ๋นผ๊ธฐ(pop),
  • ๋’ค์—์„œ ๋นผ๊ธฐ(removeLast),
  • ์–ด๋–ค ๊ฐ’ ๋ฐ”๋กœ ๋’ค์— ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ๋นผ๊ธฐ(remove(at:)).

์š” ํฌ์ŠคํŒ…์—์„œ๋Š” ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋Š” ์„ธ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ๊ฐ๊ฐ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ์‹œ๊ฐ„๋ณต์žก๋„๋„ ์•Œ์•„๋ณด๋„๋ก ํ•œ๋‹ค.

pop ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

pop์€ ์–ด๋–ค ๊ฐ’์„ LinkedList์˜ ๊ฐ€์žฅ ์•ž์—์„œ ์ œ๊ฑฐํ•œ๋‹ค. push์™€ ๋น„์Šทํ•˜๊ณ  ์‰ฝ๋‹ค.

push๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์ž.

    mutating func pop() -> T? {
        defer {
            self.head = head?.next
            if self.isEmpty { self.tail = nil }
        }
        
        return self.head?.value

pop์„ ๊ตฌํ˜„ํ•  ๋•Œ ์ œ๊ฑฐ๋˜๋Š” ๋…ธ๋“œ์˜ value๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ํ–ˆ๋‹ค. list๊ฐ€ ๋น„์–ด์žˆ์„ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— value๋Š” ์˜ต์…”๋„๋กœ ๋ฆฌํ„ด๋œ๋‹ค.

pop์€ head ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ head์˜ next ๋…ธ๋“œ๊ฐ€ head๊ฐ€ ๋˜๋Š” ์ผ์ด๋ฏ€๋กœ ARC๋Š” ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ๋๋‚˜๋ฉด pop๋œ ๋…ธ๋“œ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐํ•œ๋‹ค. (๋”์ด์ƒ ์ฐธ์กฐํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ?)

defer์—์„œ ๋ชฉ๋ก์ด ๋น„์–ด์žˆ๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ tail์„ nil๋กœ ์„ค์ •ํ•ด์„œ ํ˜น์‹œ๋‚˜ ๋‚จ์•„์žˆ์„ ๋ชจ๋“  ์ฐธ์กฐ๋ฅผ ์ œ๊ฑฐํ•ด๋ฒ„๋ฆฐ๋‹ค.

image

๋งˆ์ง€๋ง‰ insert ๋ฉ”์„œ๋“œ ์ดํ›„ pop์„ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด 2 - 10000 - 3 ๋งŒ ๋‚จ๊ฒŒ ๋œ๋‹ค.

removeLast ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

removeLast๋Š” ์•ฝ๊ฐ„ ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง„๋‹ค. tail ๋…ธ๋“œ๋ฅผ ๋นผ๊ณ  ๊ทธ ์•ž์— ๊ฐ’์„ tail๋กœ ์ง€์ •ํ•˜๋ฉด ๋˜์ง€ ์•Š๋ƒ?! ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” LinkedList๋Š” prev์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— tail ๋…ธ๋“œ์˜ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์•Œ์ง€ ๋ชปํ•œ๋‹ค.

๊ทธ๋ž˜์„œ list๋ฅผ ์ˆœํšŒํ•ด์„œ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์™€ ๊ทธ ์•ž ๋…ธ๋“œ๋ฅผ ์•Œ์•„๋‚ด์•ผ ํ•œ๋‹ค.

    mutating func removeLast() -> T? {
        guard let head = self.head else { return nil }
        
        guard head.next != nil else { return self.pop() }
        
        var prev = self.head
        var current = self.head
        
        while let next = current?.next {
            prev = current
            current = next
        }
        
        prev?.next = nil
        self.tail = prev
        
        return current?.value
    }
  • head๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” linkedlist๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ nil์„ return ํ•œ๋‹ค.
  • head.next๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ head ํ•˜๋‚˜๋งŒ ๋‚จ์•˜๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ pop์œผ๋กœ head์˜ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  • ์ดํ›„ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰๊นŒ์ง€ while๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ tail๊ณผ tail์˜ ์•ž ๋…ธ๋“œ๋ฅผ ์•Œ์•„๋‚ธ๋‹ค.
  • tail์˜ ๊ฐ’์€ return ํ•˜๊ณ  tail์˜ ์•ž ๋…ธ๋“œ๋ฅผ tail๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

image

์•ž ์ฝ”๋“œ์— ์ด์–ด์„œ removeLast๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ value์ธ 3์ด ์ œ๊ฑฐ๋˜๊ณ  2 - 10000 ์ด ๋‚จ๋Š”๋‹ค.

remove(after:) ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

remove(after:) ๋Š” ์›ํ•˜๋Š” ๋…ธ๋“œ ๋‹ค์Œ์˜ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋„๋ก ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ์ˆœ์„œ๋ฅผ ๋”ฐ์ง€๋ฉด ์ด๋ ‡๋‹ค.

  1. ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. (์›ํ•˜๋Š” ๊ฐ’์ด ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค.)
  2. ํŠน์ • ๋…ธ๋“œ ๋‹ค์Œ(.next)์„ ์ œ๊ฑฐํ•œ๋‹ค.

1๋ฒˆ์˜ ๊ฒฝ์šฐ๋Š” ์•ž์— insert(after:) ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•œ node ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    mutating func remove(after node: Node<T>) -> T? {
        defer {
            if node.next === tail {
                tail = node
            }
            
            node.next = node.next?.next
        }
        
        return node.next?.value
    }

๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” node์˜ ๋‹ค์Œ value๋ฅผ return ํ•œ๋‹ค. ๋งŒ์•ฝ ํŠน์ • ๋…ธ๋“œ์˜ ๋‹ค์Œ์ด tail์ด๋ผ๋ฉด tail์„ ์ œ๊ฑฐํ•˜๊ฒŒ ๋˜๋Š” ๊ผด์ด ๋˜๋ฏ€๋กœ tail์˜ ๊ฐ’์„ ํ˜„์žฌ node๋กœ ๋Œ€์ฒดํ•œ๋‹ค. ๊ทธ ํ›„ node์˜ ๋‹ค์Œ์€ ์ œ๊ฑฐ๋˜๋ฏ€๋กœ node.next๋Š” ๋‹ค์Œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ๋˜๋„๋ก ๋ณ€๊ฒฝํ•œ๋‹ค.

image

removeLast() ๋ฉ”์„œ๋“œ ๋‹ค์Œ์— 1, 2๋ฅผ push๋กœ ์ง‘์–ด๋„ฃ์–ด 2 - 1 - 2 - 10000 ์œผ๋กœ ๋งŒ๋“  ๋’ค 1๋ฒˆ Index์˜ ๋…ธ๋“œ ๋‹ค์Œ์„ ์ œ๊ฑฐํ•˜๋„๋ก ํ–ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ 2 - 1 - 10000 ์œผ๋กœ 1๋ฒˆ index์ธ 1 ๋’ค์˜ ๊ฐ’์ด ์ œ๊ฑฐ๋˜์—ˆ๋‹ค.

์‹œ๊ฐ„๋ณต์žก๋„ ์ •๋ฆฌ

image

pop๊ณผ remove(after:)์€ ์›ํ•˜๋Š” ์œ„์น˜์˜ ๋…ธ๋“œ๋ฅผ ํ•œ๋ฒˆ์— ์ œ๊ฑฐํ•˜๋ฏ€๋กœ O(1)์ด๊ณ  removeLast๋Š” tail๊ณผ tail์˜ ์•ž ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์•ผํ•ด์„œ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

๋‹ค์Œ์‹œ๊ฐ„์—๋Š” Remove๋กœโ€ฆ.!!!

Leave a comment