数据结构与算法系列之链表操作全集（三）（GO）

代码实现

```func (list *List) DelLastNNode1(lastN int) {
if lastN > list.Length() || lastN <= 0 {
fmt.Println("输入的待删结点位置不合法")
return
}
//删除尾结点
if lastN == 1 {
list.RemoveLastNode()
return
}
//删除头结点
if lastN == list.Length() {
//删除链表头结点
return
}
fastStep := lastN-1
for fastNode.Next != nil {
if fastStep > 0 {
fastNode = fastNode.Next
fastStep--
continue
}
fastNode = fastNode.Next
prevNode = lowNode
lowNode = lowNode.Next
}
prevNode.Next = lowNode.Next
}```

代码实现

```func (list *List) DelLastNNode2(lastN int) {
if lastN > list.Length() || lastN <= 0{
fmt.Println("输入的待删结点位置不合法")
return
}
length := list.Length()
position := length-lastN+1
if position == 1 {
//删除链表头结点
} else if position == length {
//删除链表的尾结点
list.RemoveLastNode()
} else {
//删除链表中指定位置的结点
list.RemovePosition(position)
}
}```

`https://github.com/Rain-Life/data-struct-by-go`

代码实现

```func (list *List) MergeLinkedList(list1 List, list2 List) {
fmt.Println("两个链表均为空")
return
} else if list1.HeadNode == nil {
return
} else if list2.HeadNode == nil {
return
}
if curr1.Data.(int) < curr2.Data.(int) {
curr1 = curr1.Next
} else {
curr2 = curr2.Next
}
for curr1 != nil && curr2 != nil {
if curr1.Data.(int) < curr2.Data.(int) {
currNew.Next = curr1
curr1 = curr1.Next
} else {
currNew.Next = curr2
curr2 = curr2.Next
}
currNew = currNew.Next
}
if curr1 == nil {
currNew.Next = curr2
}
if curr2 == nil {
currNew.Next = curr1
}
}```

代码实现

```func RecursionMergeList(head1 *Node, head2 *Node) *Node {
newNode := &Node{}
} else if head2 == nil {
} else {
} else {
}
return newNode
}
}```