Skip to content

Commit 3610263

Browse files
committed
[test] added new test
1 parent d16fb68 commit 3610263

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

linked-list-merge-two-sorted/linked-list-merge-two-sorted.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func New() *Node {
2929
func (n *Node) Next() *Node {
3030
return n.next
3131
}
32-
32+
3333
//Returns the last node in list if exist, otherwise returns current
3434
func (n *Node) Back() *Node {
3535
current := n.next
@@ -56,15 +56,15 @@ func Push(head_ref **Node, new_data int) {
5656
*head_ref = new_node
5757
}
5858

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
6060
/* MoveNode() function takes the node from the front of the
6161
   source, and move it to the front of the dest.
6262
   It is an error to call this with the source list empty.
63-
 
63+
6464
   Before calling MoveNode():
6565
   source == {1, 2, 3}
6666
   dest == {1, 2, 3}
67-
 
67+
6868
   Affter calling MoveNode():
6969
   source == {2, 3}
7070
   dest == {1, 1, 2, 3} */
@@ -83,48 +83,58 @@ func MoveNode(dest_ref **Node, source_ref **Node) {
8383
}
8484

8585
/* 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.  */
8888
func SortedMerge(a *Node, b *Node) *Node {
8989
//A dummy first node to hang the result on
9090
dummy := New()
9191

9292
//Tail points to the last result node
9393
tail := dummy
94-
94+
9595
//So tail.next is the place to add new nodes to result
9696
dummy.next = nil
9797

9898
for {
99-
if (a == nil){
99+
if a == nil {
100100
//If either list runs out, use the other list
101101
tail.next = b
102102
break
103-
} else if (b == nil){
103+
} else if b == nil {
104104
tail.next = a
105105
break
106106
}
107107

108-
if (a.data <= b.data){
108+
if a.data <= b.data {
109109
MoveNode(&(tail.next), &a)
110-
} else{
110+
} else {
111111
MoveNode(&(tail.next), &b)
112112
}
113113

114114
tail = tail.next
115115
}
116-
116+
117117
return dummy.next
118118
}
119119

120120
//This function prints contents of linked list starting from the given node
121-
func printList(n *Node){
121+
func printList(n *Node) {
122122
for n != nil {
123-
fmt.Println(n.data)
123+
fmt.Print(n.data)
124+
fmt.Print(" ,")
124125
n = n.next
125126
}
126127
}
127128

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+
128138
func main() {
129139
//Start with the empty list
130140
res := New()
@@ -144,4 +154,4 @@ func main() {
144154

145155
fmt.Println("Merged LinkedList is:")
146156
printList(res)
147-
}
157+
}

0 commit comments

Comments
 (0)