-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathContents.swift
56 lines (44 loc) · 1.32 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Foundation
precedencegroup LongArrowPrecedence {
associativity: left
higherThan: AssignmentPrecedence
}
infix operator ~>: LongArrowPrecedence
class Node: CustomStringConvertible{
var description: String {
if next == nil { return "\(value)" }
return "\(value) ~> \(next!.description)"
}
var next: Node?
var value: Int
init(_ value: Int) {
self.value = value
}
static func ~>(lhs: Node, rhs: Node) -> Node {
lhs.next = rhs
return rhs
}
}
/*
Asked in: Accolite Adobe Amazon Citicorp Epic Systems MAQ Software Monotype Solutions Snapdeal
Given a linked list, the task is to find the n'th node from the end. It is needed to complete a method that takes two argument, head of linked list and an integer n. There are multiple test cases. For each test case, this method will be called individually.
*/
//fastest approach
//time O(n)
//space O(1)
func nNode(in head: Node, n: Int) -> Int {
var current = head
var count = 0
var nNode = head
while let next = current.next {
count += 1
if count >= n {
nNode = nNode.next!
}
current = next
}
return nNode.value
}
let head = Node(1)
head ~> Node(2) ~> Node(3) ~> Node(4) ~> Node(5)
nNode(in: head, n: 2)