-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path826.linkedlist.go
64 lines (50 loc) · 865 Bytes
/
826.linkedlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import "fmt"
type Node struct {
Val int
Next *Node
}
type LinkedList struct {
Head *Node
}
func main() {
m := 0
fmt.Scanf("%d", &m)
lk := &LinkedList{&Node{Val: 0, Next: nil}}
for ; m > 0; m-- {
var t string
var p, q int
fmt.Scanf("%s%d%d", &t, &p, &q)
switch t {
case "H":
HeadInsert(lk, p)
case "I":
Insert(lk, p, q)
case "D":
Del(lk, p)
}
}
PrintLk(lk)
}
func PrintLk(lk *LinkedList) {
p := lk.Head
for p != nil {
fmt.Printf("%d ", p.Val)
if p.Next != nil {
p = p.Next
}
}
}
//HeadInsert insert x after the head
func HeadInsert(lk *LinkedList, x int) {
head := lk.Head
node := &Node{Val: x, Next: nil}
node.Next = head.Next
head.Next = node
}
//Insert after the kth insert with value x
func Insert(lk *LinkedList, k, x int) {
}
//Del the kth elem
func Del(lk *LinkedList, k int) {
}