Skip to content

Commit

Permalink
Add go solution for 206-Reverse-Linked-List
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKheng committed Sep 17, 2022
1 parent fd08e46 commit 28c81d0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions go/206-Reverse-Linked-List.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}

reversedListHead := reverseList(head.Next)
head.Next.Next = head
head.Next = nil
return reversedListHead
}

// Iterative version
// func reverseList(head *ListNode) *ListNode {
// var prev *ListNode
// curr := head
//
// for curr != nil {
// tmp := curr.Next
// curr.Next = prev
// prev = curr
// curr = tmp
// }
//
// return prev
// }

0 comments on commit 28c81d0

Please sign in to comment.