mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
@ -948,72 +948,91 @@ class MyLinkedList {
|
||||
}
|
||||
```
|
||||
|
||||
Swift
|
||||
```Swift
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
class MyLinkedList {
|
||||
var size = 0
|
||||
let head: ListNode
|
||||
var dummyHead: ListNode<Int>?
|
||||
var size: Int
|
||||
|
||||
/** Initialize your data structure here. */
|
||||
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 {
|
||||
if size > 0 && index < size {
|
||||
var tempHead = head
|
||||
for _ in 0..<index {
|
||||
tempHead = tempHead.next!
|
||||
}
|
||||
let currentNode = tempHead.next!
|
||||
return currentNode.val
|
||||
} else {
|
||||
if index >= size || index < 0 {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
if index > size {
|
||||
return
|
||||
}
|
||||
let idx = (index >= 0 ? index : 0)
|
||||
var tempHead = head
|
||||
for _ in 0 ..< idx {
|
||||
tempHead = tempHead.next!
|
||||
|
||||
let newNode = ListNode(val)
|
||||
var curNode = dummyHead
|
||||
var curIndex = index
|
||||
|
||||
while curIndex > 0 {
|
||||
curNode = curNode?.next
|
||||
curIndex -= 1
|
||||
}
|
||||
let currentNode = tempHead.next
|
||||
let newNode = ListNode(val, currentNode)
|
||||
tempHead.next = newNode
|
||||
|
||||
newNode.next = curNode?.next
|
||||
curNode?.next = newNode
|
||||
size += 1
|
||||
}
|
||||
|
||||
/** Delete the index-th node in the linked list, if the index is valid. */
|
||||
func deleteAtIndex(_ index: Int) {
|
||||
if size > 0 && index < size {
|
||||
var tempHead = head
|
||||
for _ in 0 ..< index {
|
||||
tempHead = tempHead.next!
|
||||
}
|
||||
tempHead.next = tempHead.next!.next
|
||||
size -= 1
|
||||
if index >= size || index < 0 {
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
Reference in New Issue
Block a user