Created
January 1, 2026 14:22
-
-
Save rtsoy/f294417dfc360d9cc5c285c5de932011 to your computer and use it in GitHub Desktop.
206. Reverse Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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