Add solution 1461、1721

This commit is contained in:
YDZ
2021-03-15 10:47:36 +08:00
parent 943d558ca8
commit c30c6c7f41
32 changed files with 855 additions and 280 deletions

View File

@ -0,0 +1,36 @@
package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func swapNodes(head *ListNode, k int) *ListNode {
count := 1
var a, b *ListNode
for node := head; node != nil; node = node.Next {
if count == k {
a = node
}
count++
}
length := count
count = 1
for node := head; node != nil; node = node.Next {
if count == length-k {
b = node
}
count++
}
a.Val, b.Val = b.Val, a.Val
return head
}