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

Function reverseList1

reverse_linked_list_206/solution.go:34–57  ·  view source on GitHub ↗

Reverses the passed list without creating a new list by prepending front nodes to the end.

(head *ListNode)

Source from the content-addressed store, hash-verified

32// Reverses the passed list without creating a new list by
33// prepending front nodes to the end.
34func reverseList1(head *ListNode) *ListNode {
35 if head == nil {
36 return nil
37 }
38
39 // find the end of the list
40 end := head
41 for end.Next != nil {
42 end = end.Next
43 }
44
45 // prepend every front node to the end
46 // 1,2,3
47 // 2,3,1
48 // 3,2,1
49 for end != head {
50 hold := head
51 head = head.Next
52 hold.Next = end.Next
53 end.Next = hold
54 }
55
56 return head
57}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected