mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
@ -147,6 +147,35 @@ Python:
|
||||
|
||||
Go:
|
||||
|
||||
```go
|
||||
//双指针
|
||||
func reverseList(head *ListNode) *ListNode {
|
||||
var pre *ListNode
|
||||
cur := head
|
||||
for cur != nil {
|
||||
next := cur.Next
|
||||
cur.Next = pre
|
||||
pre = cur
|
||||
cur = next
|
||||
}
|
||||
return pre
|
||||
}
|
||||
|
||||
//递归
|
||||
func reverseList(head *ListNode) *ListNode {
|
||||
return help(nil, head)
|
||||
}
|
||||
|
||||
func help(pre, head *ListNode)*ListNode{
|
||||
if head == nil {
|
||||
return pre
|
||||
}
|
||||
next := head.Next
|
||||
head.Next = pre
|
||||
return help(head, next)
|
||||
}
|
||||
|
||||
```
|
||||
javaScript:
|
||||
|
||||
```js
|
||||
|
@ -393,6 +393,127 @@ class MyLinkedList:
|
||||
|
||||
Go:
|
||||
|
||||
```go
|
||||
//循环双链表
|
||||
type MyLinkedList struct {
|
||||
dummy *Node
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Val int
|
||||
Next *Node
|
||||
Pre *Node
|
||||
}
|
||||
|
||||
//仅保存哑节点,pre-> rear, next-> head
|
||||
/** Initialize your data structure here. */
|
||||
func Constructor() MyLinkedList {
|
||||
rear := &Node{
|
||||
Val: -1,
|
||||
Next: nil,
|
||||
Pre: nil,
|
||||
}
|
||||
rear.Next = rear
|
||||
rear.Pre = rear
|
||||
return MyLinkedList{rear}
|
||||
}
|
||||
|
||||
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
|
||||
func (this *MyLinkedList) Get(index int) int {
|
||||
head := this.dummy.Next
|
||||
//head == this, 遍历完全
|
||||
for head != this.dummy && index > 0 {
|
||||
index--
|
||||
head = head.Next
|
||||
}
|
||||
//否则, head == this, 索引无效
|
||||
if 0 != index {
|
||||
return -1
|
||||
}
|
||||
return head.Val
|
||||
}
|
||||
|
||||
/** 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 (this *MyLinkedList) AddAtHead(val int) {
|
||||
dummy := this.dummy
|
||||
node := &Node{
|
||||
Val: val,
|
||||
//head.Next指向原头节点
|
||||
Next: dummy.Next,
|
||||
//head.Pre 指向哑节点
|
||||
Pre: dummy,
|
||||
}
|
||||
|
||||
//更新原头节点
|
||||
dummy.Next.Pre = node
|
||||
//更新哑节点
|
||||
dummy.Next = node
|
||||
//以上两步不能反
|
||||
}
|
||||
|
||||
/** Append a node of value val to the last element of the linked list. */
|
||||
func (this *MyLinkedList) AddAtTail(val int) {
|
||||
dummy := this.dummy
|
||||
rear := &Node{
|
||||
Val: val,
|
||||
//rear.Next = dummy(哑节点)
|
||||
Next: dummy,
|
||||
//rear.Pre = ori_rear
|
||||
Pre: dummy.Pre,
|
||||
}
|
||||
|
||||
//ori_rear.Next = rear
|
||||
dummy.Pre.Next = rear
|
||||
//update dummy
|
||||
dummy.Pre = rear
|
||||
//以上两步不能反
|
||||
}
|
||||
|
||||
/** 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 (this *MyLinkedList) AddAtIndex(index int, val int) {
|
||||
head := this.dummy.Next
|
||||
//head = MyLinkedList[index]
|
||||
for head != this.dummy && index > 0 {
|
||||
head = head.Next
|
||||
index--
|
||||
}
|
||||
node := &Node{
|
||||
Val: val,
|
||||
//node.Next = MyLinkedList[index]
|
||||
Next: head,
|
||||
//node.Pre = MyLinkedList[index-1]
|
||||
Pre: head.Pre,
|
||||
}
|
||||
//MyLinkedList[index-1].Next = node
|
||||
head.Pre.Next = node
|
||||
//MyLinkedList[index].Pre = node
|
||||
head.Pre = node
|
||||
//以上两步不能反
|
||||
}
|
||||
|
||||
/** Delete the index-th node in the linked list, if the index is valid. */
|
||||
func (this *MyLinkedList) DeleteAtIndex(index int) {
|
||||
//链表为空
|
||||
if this.dummy.Next == this.dummy {
|
||||
return
|
||||
}
|
||||
head := this.dummy.Next
|
||||
//head = MyLinkedList[index]
|
||||
for head.Next != this.dummy && index > 0 {
|
||||
head = head.Next
|
||||
index--
|
||||
}
|
||||
//验证index有效
|
||||
if index == 0 {
|
||||
//MyLinkedList[index].Pre = index[index-2]
|
||||
head.Next.Pre = head.Pre
|
||||
//MyLinedList[index-2].Next = index[index]
|
||||
head.Pre.Next = head.Next
|
||||
//以上两步顺序无所谓
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
javaScript:
|
||||
|
||||
```js
|
||||
|
Reference in New Issue
Block a user