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)
| 6 | // This function prepends the first n values in the passed list |
| 7 | // into a new list and returns the new list. |
| 8 | func 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. |
no outgoing calls