Skip to content

Instantly share code, notes, and snippets.

@rtsoy
Created January 1, 2026 14:22
Show Gist options
  • Select an option

  • Save rtsoy/f294417dfc360d9cc5c285c5de932011 to your computer and use it in GitHub Desktop.

Select an option

Save rtsoy/f294417dfc360d9cc5c285c5de932011 to your computer and use it in GitHub Desktop.
206. Reverse Linked List
// https://leetcode.com/problems/reverse-linked-list/
//
// Time: O(N)
// Space: O(1)
//
// N = number of nodes in the list
// .................... //
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseList(head *ListNode) *ListNode {
curr, prev := head, (*ListNode)(nil)
for curr != nil {
next := curr.Next
curr.Next = prev
prev = curr
curr = next
}
return prev
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment