MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / reverseList

Function reverseList

reverse_linked_list_206/solution.go:8–30  ·  view source on GitHub ↗

Returns a pointer to a list that is the reverse of the passed head. This function prepends the first n values in the passed list into a new list and returns the new list.

(head *ListNode)

Source from the content-addressed store, hash-verified

6// This function prepends the first n values in the passed list
7// into a new list and returns the new list.
8func reverseList(head *ListNode) *ListNode {
9 if head == nil {
10 return nil
11 }
12
13 var rev *ListNode
14 for head != nil {
15 // hold last head
16 hold := head
17 head = head.Next
18
19 // prepend into rev
20 if rev == nil {
21 rev = hold
22 rev.Next = nil
23 } else {
24 hold.Next = rev
25 rev = hold
26 }
27 }
28
29 return rev
30}
31
32// Reverses the passed list without creating a new list by
33// prepending front nodes to the end.

Callers 1

Test_reverseListFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_reverseListFunction · 0.68