Skip to content

Commit

Permalink
19-remove-nth-node-from-end-of-list
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 11, 2018
1 parent 0d10f5e commit 812b2e7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
68 changes: 68 additions & 0 deletions 19-remove-nth-node-from-end-of-list.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/// 19. Remove Nth Node From End of List
/// Given a linked list, remove the n-th node from the end of list and return its head.
///
/// Example:
/// Given linked list: 1->2->3->4->5, and n = 2.
///
/// After removing the second node from the end, the linked list becomes 1->2->3->5.
/// - Note: Given n will always be valid.

import XCTest

/// - Warning: Swift judge for this problem isn't working. So the solution below hasn't been tested
/// on LeetCode.com yet.
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
var nodes: [ListNode] = []
var next = head
while next != nil {
nodes.append(next!)
next = next?.next
}
let idx = nodes.count - n
guard idx > 0 else {
head?.next = nil
return nodes[1] // n will always be valid
}
nodes[idx - 1].next = nodes[idx].next
nodes[idx].next = nil
return head
}

final class ListNode {
var val: Int
var next: ListNode?
init(_ val: Int) {
self.val = val
}

init(array: [Int]) {
guard !array.isEmpty else { fatalError() }
val = array[0]
var previous = self
for i in 1..<array.count {
let new = ListNode(array[i])
previous.next = new
previous = new
}
}

func toArray() -> [Int] {
var current: ListNode? = self
var array: [Int] = []
while current != nil {
array.append(current!.val)
current = current?.next
}
return array
}
}

class Tests: XCTestCase {
func testExample() {
let head = ListNode(array: [1, 2, 3, 4, 5])
let result = removeNthFromEnd(head, 2)
XCTAssertEqual(result?.toArray(), [1, 2, 3, 5])
}
}

Tests.defaultTestSuite.run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>

0 comments on commit 812b2e7

Please sign in to comment.