@@ -29,7 +29,7 @@ func New() *Node {
29
29
func (n * Node ) Next () * Node {
30
30
return n .next
31
31
}
32
-
32
+
33
33
//Returns the last node in list if exist, otherwise returns current
34
34
func (n * Node ) Back () * Node {
35
35
current := n .next
@@ -56,15 +56,15 @@ func Push(head_ref **Node, new_data int) {
56
56
* head_ref = new_node
57
57
}
58
58
59
- //Pull off the front node of the source and put it in dest
59
+ //Pull off the front node of the source and put it in dest
60
60
/* MoveNode() function takes the node from the front of the
61
61
source, and move it to the front of the dest.
62
62
It is an error to call this with the source list empty.
63
-
63
+
64
64
Before calling MoveNode():
65
65
source == {1, 2, 3}
66
66
dest == {1, 2, 3}
67
-
67
+
68
68
Affter calling MoveNode():
69
69
source == {2, 3}
70
70
dest == {1, 1, 2, 3} */
@@ -83,48 +83,58 @@ func MoveNode(dest_ref **Node, source_ref **Node) {
83
83
}
84
84
85
85
/* Takes two lists sorted in increasing order, and splices
86
- their nodes together to make one big sorted list which
87
- is returned. */
86
+ their nodes together to make one big sorted list which
87
+ is returned. */
88
88
func SortedMerge (a * Node , b * Node ) * Node {
89
89
//A dummy first node to hang the result on
90
90
dummy := New ()
91
91
92
92
//Tail points to the last result node
93
93
tail := dummy
94
-
94
+
95
95
//So tail.next is the place to add new nodes to result
96
96
dummy .next = nil
97
97
98
98
for {
99
- if ( a == nil ) {
99
+ if a == nil {
100
100
//If either list runs out, use the other list
101
101
tail .next = b
102
102
break
103
- } else if ( b == nil ) {
103
+ } else if b == nil {
104
104
tail .next = a
105
105
break
106
106
}
107
107
108
- if ( a .data <= b .data ) {
108
+ if a .data <= b .data {
109
109
MoveNode (& (tail .next ), & a )
110
- } else {
110
+ } else {
111
111
MoveNode (& (tail .next ), & b )
112
112
}
113
113
114
114
tail = tail .next
115
115
}
116
-
116
+
117
117
return dummy .next
118
118
}
119
119
120
120
//This function prints contents of linked list starting from the given node
121
- func printList (n * Node ){
121
+ func printList (n * Node ) {
122
122
for n != nil {
123
- fmt .Println (n .data )
123
+ fmt .Print (n .data )
124
+ fmt .Print (" ," )
124
125
n = n .next
125
126
}
126
127
}
127
128
129
+ func GetDataList (n * Node ) []int {
130
+ data := []int {}
131
+ for n != nil {
132
+ data = append (data , n .data )
133
+ n = n .next
134
+ }
135
+ return data
136
+ }
137
+
128
138
func main () {
129
139
//Start with the empty list
130
140
res := New ()
@@ -144,4 +154,4 @@ func main() {
144
154
145
155
fmt .Println ("Merged LinkedList is:" )
146
156
printList (res )
147
- }
157
+ }
0 commit comments