-
Notifications
You must be signed in to change notification settings - Fork 2
/
linkedList_helper.go
67 lines (60 loc) · 1.12 KB
/
linkedList_helper.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
65
66
67
package leetcode
import (
"fmt"
"strconv"
"strings"
)
type ListNode struct {
Val string
Next *ListNode
}
type IntListNode struct {
Val int
Next *IntListNode
}
func ParseLinkedListFromStr(input string) *ListNode {
arr := strings.Split(input, `,`)
if len(arr) == 0 {
return nil
}
node := &ListNode{arr[0], nil}
p := node
for i := 1; i < len(arr); i++ {
p.Next = &ListNode{arr[i], nil}
p = p.Next
}
return node
}
func ParseIntLinkedListFromStr(input string) *IntListNode {
arr := strings.Split(input, `->`)
if len(arr) == 0 {
return nil
}
intArr := make([]int, len(arr)-1)
for i := 0; i < len(arr)-1; i++ {
t, err := strconv.Atoi(arr[i])
if err != nil {
panic(err)
}
intArr[i] = t
}
node := &IntListNode{intArr[0], nil}
p := node
for i := 1; i < len(arr)-1; i++ {
p.Next = &IntListNode{intArr[i], nil}
p = p.Next
}
return node
}
func PrintIntLinkedListNode(node *IntListNode) {
for p := node; p != nil; p = p.Next {
fmt.Printf("%d->", p.Val)
}
fmt.Println()
}
func PrintLinkedListNode(node *ListNode) {
for p := node; p != nil; p = p.Next {
fmt.Printf("%s->", p.Val)
}
fmt.Println()
}