-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlinked-list-merge-two-sorted.go
157 lines (125 loc) · 3.14 KB
/
linked-list-merge-two-sorted.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
next *Node
}
//Returns an initialized list
func (n *Node) Init() *Node {
n.data = -1
return n
}
//Returns an new list
func New() *Node {
return new(Node).Init()
}
//Returns the first node in list
func (n *Node) Next() *Node {
return n.next
}
//Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node {
current := n.next
for current != nil && current.next != nil {
current = current.next
}
return current
}
func Push(head_ref **Node, new_data int) {
//new_node := Node{data: new_data, next: (*head_ref)}
//*head_ref = new_node
//1. Allocate new node
new_node := New()
//2. Put in the data
new_node.data = new_data
//3. Make next of new node as head
new_node.next = (*head_ref)
//4. Move the head to point to new_node
*head_ref = new_node
}
//Pull off the front node of the source and put it in dest
/* MoveNode() function takes the node from the front of the
source, and move it to the front of the dest.
It is an error to call this with the source list empty.
Before calling MoveNode():
source == {1, 2, 3}
dest == {1, 2, 3}
Affter calling MoveNode():
source == {2, 3}
dest == {1, 1, 2, 3} */
func MoveNode(dest_ref **Node, source_ref **Node) {
//The front source code
new_node := *source_ref
//Advance the source pointer
*source_ref = new_node.next
//Link the old dest off the new node
new_node.next = *dest_ref
//Move dest to point to the new node
*dest_ref = new_node
}
/* Takes two lists sorted in increasing order, and splices
their nodes together to make one big sorted list which
is returned. */
func SortedMerge(a *Node, b *Node) *Node {
//A dummy first node to hang the result on
dummy := New()
//Tail points to the last result node
tail := dummy
//So tail.next is the place to add new nodes to result
dummy.next = nil
for {
if a == nil {
//If either list runs out, use the other list
tail.next = b
break
} else if b == nil {
tail.next = a
break
}
if a.data <= b.data {
MoveNode(&(tail.next), &a)
} else {
MoveNode(&(tail.next), &b)
}
tail = tail.next
}
return dummy.next
}
//This function prints contents of linked list starting from the given node
func printList(n *Node) {
for n != nil {
fmt.Print(n.data)
fmt.Print(" ,")
n = n.next
}
}
func GetDataList(n *Node) []int {
data := []int{}
for n != nil {
data = append(data, n.data)
n = n.next
}
return data
}
func main() {
//Start with the empty list
res := New()
a := New()
b := New()
Push(&a, 15)
Push(&a, 10)
Push(&a, 5)
Push(&b, 20)
Push(&b, 3)
Push(&b, 2)
//Remove duplicates from LinkedList
res = SortedMerge(a, b)
fmt.Println("Merged LinkedList is:")
printList(res)
}