mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
@ -948,72 +948,91 @@ class MyLinkedList {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift
|
|
||||||
```Swift
|
|
||||||
class MyLinkedList {
|
|
||||||
var size = 0
|
|
||||||
let head: ListNode
|
|
||||||
|
|
||||||
/** Initialize your data structure here. */
|
Swift:
|
||||||
|
|
||||||
|
```swift
|
||||||
|
class MyLinkedList {
|
||||||
|
var dummyHead: ListNode<Int>?
|
||||||
|
var size: Int
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
head = ListNode(-1)
|
dummyHead = ListNode(0)
|
||||||
|
size = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
|
|
||||||
func get(_ index: Int) -> Int {
|
func get(_ index: Int) -> Int {
|
||||||
if size > 0 && index < size {
|
if index >= size || index < 0 {
|
||||||
var tempHead = head
|
|
||||||
for _ in 0..<index {
|
|
||||||
tempHead = tempHead.next!
|
|
||||||
}
|
|
||||||
let currentNode = tempHead.next!
|
|
||||||
return currentNode.val
|
|
||||||
} else {
|
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var curNode = dummyHead?.next
|
||||||
|
var curIndex = index
|
||||||
|
|
||||||
|
while curIndex > 0 {
|
||||||
|
curNode = curNode?.next
|
||||||
|
curIndex -= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return curNode?.value ?? -1
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
|
|
||||||
func addAtHead(_ val: Int) {
|
func addAtHead(_ val: Int) {
|
||||||
addAtIndex(0, val)
|
let newHead = ListNode(val)
|
||||||
|
newHead.next = dummyHead?.next
|
||||||
|
dummyHead?.next = newHead
|
||||||
|
size += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Append a node of value val to the last element of the linked list. */
|
|
||||||
func addAtTail(_ val: Int) {
|
func addAtTail(_ val: Int) {
|
||||||
addAtIndex(size, val)
|
let newNode = ListNode(val)
|
||||||
|
var curNode = dummyHead
|
||||||
|
while curNode?.next != nil {
|
||||||
|
curNode = curNode?.next
|
||||||
|
}
|
||||||
|
|
||||||
|
curNode?.next = newNode
|
||||||
|
size += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
|
|
||||||
func addAtIndex(_ index: Int, _ val: Int) {
|
func addAtIndex(_ index: Int, _ val: Int) {
|
||||||
if index > size {
|
if index > size {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let idx = (index >= 0 ? index : 0)
|
|
||||||
var tempHead = head
|
let newNode = ListNode(val)
|
||||||
for _ in 0 ..< idx {
|
var curNode = dummyHead
|
||||||
tempHead = tempHead.next!
|
var curIndex = index
|
||||||
|
|
||||||
|
while curIndex > 0 {
|
||||||
|
curNode = curNode?.next
|
||||||
|
curIndex -= 1
|
||||||
}
|
}
|
||||||
let currentNode = tempHead.next
|
|
||||||
let newNode = ListNode(val, currentNode)
|
newNode.next = curNode?.next
|
||||||
tempHead.next = newNode
|
curNode?.next = newNode
|
||||||
size += 1
|
size += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete the index-th node in the linked list, if the index is valid. */
|
|
||||||
func deleteAtIndex(_ index: Int) {
|
func deleteAtIndex(_ index: Int) {
|
||||||
if size > 0 && index < size {
|
if index >= size || index < 0 {
|
||||||
var tempHead = head
|
return
|
||||||
for _ in 0 ..< index {
|
|
||||||
tempHead = tempHead.next!
|
|
||||||
}
|
|
||||||
tempHead.next = tempHead.next!.next
|
|
||||||
size -= 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var curNode = dummyHead
|
||||||
|
for _ in 0..<index {
|
||||||
|
curNode = curNode?.next
|
||||||
|
}
|
||||||
|
|
||||||
|
curNode?.next = curNode?.next?.next
|
||||||
|
size -= 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
Reference in New Issue
Block a user