public/subjects/changeorder/README.md

61 lines
961 B
Markdown
Raw Permalink Normal View History

2019-12-05 12:30:02 +00:00
## changeorder
### Instructions
You are given a linked list, where each node contains a single digit.
Change order of linked list so that elements with odd index come first, elements with even index come afterwards.
2019-12-05 12:30:02 +00:00
You have to return pointer/reference to the beginning of new list
2019-12-05 13:21:44 +00:00
### Expected function and structure
2019-12-05 12:30:02 +00:00
```go
2019-12-05 13:21:44 +00:00
package main
2019-12-05 12:30:02 +00:00
2019-12-05 14:15:27 +00:00
type NodeAddL struct {
2020-02-25 12:02:16 +00:00
Next *NodeAddL
Num int
2019-12-05 12:30:02 +00:00
}
2019-12-05 14:15:27 +00:00
func Changeorder(node *NodeAddL) *NodeAddL {
2019-12-05 12:30:02 +00:00
}
```
### Usage
2019-12-05 13:21:44 +00:00
2019-12-05 12:30:02 +00:00
Here is a possible program to test your function:
```go
package main
import "fmt"
2019-12-05 12:30:02 +00:00
// I implemented pushBack for this
func main() {
2020-02-25 12:02:16 +00:00
num1 := &NodeAddL{Num: 1}
num1 = pushBack(num1, 2)
num1 = pushBack(num1, 3)
num1 = pushBack(num1, 4)
num1 = pushBack(num1, 5)
result := Changeorder(num1)
for tmp := result; tmp != nil; tmp = tmp.Next {
fmt.Print(tmp.Num)
if tmp.Next != nil {
fmt.Print(" -> ")
}
}
fmt.Println()
2019-12-05 12:30:02 +00:00
}
```
Its output:
```console
$ go run .
2019-12-05 12:30:02 +00:00
1 -> 3 -> 5 -> 2 -> 4
$
2019-12-05 12:30:02 +00:00
```