GOLANG_INTERVIEW Telegram 313
👣 Сколько времени в минутах займет у вас написание процедуры обращения односвязного списка?

package main

import "fmt"

type Node struct {
Val int
Next *Node
}

func reverseLinkedList(head *Node) *Node {
var prev *Node
curr := head
for curr != nil {
Next := curr.Next
curr.Next = prev
prev = curr
curr = Next
}
return prev
}

// можно сократить curr
// func reverseLinkedList(head *Node) *Node {
// var prev *Node
// for head != nil {
// Next := head.Next
// head.Next = prev
// prev = head
// head = Next
// }
// return prev
// }

// можно сократить Next (через множественное присваивание)
// func reverseLinkedList(head *Node) *Node {
// var prev *Node
// for head != nil {
// head.Next, prev, head = prev, head, head.Next
// }
// return prev
// }

// или создавая новые узлы
// func reverseLinkedList(head *Node) *Node {
// var result *Node
// curr := head
// for curr != nil {
// result = &Node{curr.Val, result}
// curr = curr.Next
// }
// return result
// }

func printLinkedList(head *Node) {
current := head
for current != nil {
fmt.Printf("%d -> ", current.Val)
current = current.Next
}
fmt.Println("nil")
}

func main() {
node1 := &Node{Val: 1}
node2 := &Node{Val: 2}
node3 := &Node{Val: 3}
node4 := &Node{Val: 4}
node5 := &Node{Val: 5}
node1.Next = node2
node2.Next = node3
node3.Next = node4
node4.Next = node5
fmt.Println("Исходный список:")
printLinkedList(node1)
reversedHead := reverseLinkedList(node1)
fmt.Println("Обращенный список:")
printLinkedList(reversedHead)
}


Пишите свое решение в комментариях👇

@golang_interview
Please open Telegram to view this post
VIEW IN TELEGRAM
👍111🔥1



tgoop.com/golang_interview/313
Create:
Last Update:

👣 Сколько времени в минутах займет у вас написание процедуры обращения односвязного списка?

package main

import "fmt"

type Node struct {
Val int
Next *Node
}

func reverseLinkedList(head *Node) *Node {
var prev *Node
curr := head
for curr != nil {
Next := curr.Next
curr.Next = prev
prev = curr
curr = Next
}
return prev
}

// можно сократить curr
// func reverseLinkedList(head *Node) *Node {
// var prev *Node
// for head != nil {
// Next := head.Next
// head.Next = prev
// prev = head
// head = Next
// }
// return prev
// }

// можно сократить Next (через множественное присваивание)
// func reverseLinkedList(head *Node) *Node {
// var prev *Node
// for head != nil {
// head.Next, prev, head = prev, head, head.Next
// }
// return prev
// }

// или создавая новые узлы
// func reverseLinkedList(head *Node) *Node {
// var result *Node
// curr := head
// for curr != nil {
// result = &Node{curr.Val, result}
// curr = curr.Next
// }
// return result
// }

func printLinkedList(head *Node) {
current := head
for current != nil {
fmt.Printf("%d -> ", current.Val)
current = current.Next
}
fmt.Println("nil")
}

func main() {
node1 := &Node{Val: 1}
node2 := &Node{Val: 2}
node3 := &Node{Val: 3}
node4 := &Node{Val: 4}
node5 := &Node{Val: 5}
node1.Next = node2
node2.Next = node3
node3.Next = node4
node4.Next = node5
fmt.Println("Исходный список:")
printLinkedList(node1)
reversedHead := reverseLinkedList(node1)
fmt.Println("Обращенный список:")
printLinkedList(reversedHead)
}


Пишите свое решение в комментариях👇

@golang_interview

BY Golang вопросы собеседований




Share with your friend now:
tgoop.com/golang_interview/313

View MORE
Open in Telegram


Telegram News

Date: |

During a meeting with the president of the Supreme Electoral Court (TSE) on June 6, Telegram's Vice President Ilya Perekopsky announced the initiatives. According to the executive, Brazil is the first country in the world where Telegram is introducing the features, which could be expanded to other countries facing threats to democracy through the dissemination of false content. You can invite up to 200 people from your contacts to join your channel as the next step. Select the users you want to add and click “Invite.” You can skip this step altogether. Ng, who had pleaded not guilty to all charges, had been detained for more than 20 months. His channel was said to have contained around 120 messages and photos that incited others to vandalise pro-government shops and commit criminal damage targeting police stations. best-secure-messaging-apps-shutterstock-1892950018.jpg “[The defendant] could not shift his criminal liability,” Hui said.
from us


Telegram Golang вопросы собеседований
FROM American