Skip to content

Commit 71ae5a3

Browse files
committed
Renamed variables in insert methods and code refactoring.
1 parent dc810d2 commit 71ae5a3

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,17 @@ public final class LinkedList<T> {
144144
/// - node: The node containing the value to be inserted
145145
/// - index: Integer value of the index to be inserted at
146146
public func insert(_ node: Node, atIndex index: Int) {
147-
let newNode = LinkedListNode(value: node.value)
147+
let newNode = node
148148
if index == 0 {
149149
newNode.next = head
150150
head?.previous = newNode
151151
head = newNode
152152
} else {
153-
let separator = self.node(atIndex: index-1)
154-
newNode.previous = separator
155-
newNode.next = separator.next
156-
separator.next?.previous = newNode
157-
separator.next = newNode
153+
let prev = self.node(atIndex: index-1)
154+
newNode.previous = prev
155+
newNode.next = prev.next
156+
prev.next?.previous = newNode
157+
prev.next = newNode
158158
}
159159
}
160160

@@ -171,11 +171,11 @@ public final class LinkedList<T> {
171171
head = list.head
172172
list.last?.next = temp
173173
} else {
174-
let separate = self.node(atIndex: index-1)
175-
let temp = separate.next
174+
let prev = self.node(atIndex: index-1)
175+
let temp = prev.next
176176

177-
separate.next = list.head
178-
list.head?.previous = separate
177+
prev.next = list.head
178+
list.head?.previous = prev
179179

180180
list.last?.next = temp
181181
temp?.previous = list.last?.next

Linked List/LinkedList.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ public final class LinkedList<T> {
101101
}
102102

103103
public func insert(_ node: Node, atIndex index: Int) {
104-
let newNode = LinkedListNode(value: node.value)
104+
let newNode = node
105105
if index == 0 {
106106
newNode.next = head
107107
head?.previous = newNode
108108
head = newNode
109109
} else {
110-
let separator = self.node(atIndex: index-1)
111-
newNode.previous = separator
112-
newNode.next = separator.next
113-
separator.next?.previous = newNode
114-
separator.next = newNode
110+
let prev = self.node(atIndex: index-1)
111+
newNode.previous = prev
112+
newNode.next = prev.next
113+
prev.next?.previous = newNode
114+
prev.next = newNode
115115
}
116116
}
117117

@@ -123,11 +123,11 @@ public final class LinkedList<T> {
123123
head = list.head
124124
list.last?.next = temp
125125
} else {
126-
let separate = self.node(atIndex: index-1)
127-
let temp = separate.next
126+
let prev = self.node(atIndex: index-1)
127+
let temp = prev.next
128128

129-
separate.next = list.head
130-
list.head?.previous = separate
129+
prev.next = list.head
130+
list.head?.previous = prev
131131

132132
list.last?.next = temp
133133
temp?.previous = list.last?.next

0 commit comments

Comments
 (0)