-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathreverse_linked_list.go
77 lines (64 loc) · 2.8 KB
/
reverse_linked_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Implementation of reversing a linked list recursively and iteratively
/*
In this code, we define a ListNode struct which represents a node in the linked list. We then define two functions:
ReverseListIteratively and ReverseListRecursively which reverse the linked list iteratively and recursively, respectively.
In ReverseListIteratively, we declare two pointers prev and curr, initialize curr to head, and iterate over the
linked list until curr becomes nil. In each iteration, we store the Next pointer of curr in a temporary variable
nextTemp, point curr.Next to prev, update prev to curr, and update curr to nextTemp. Finally, we return prev,
which now points to the new head of the reversed linked list.
In ReverseListRecursively, we first check if head is nil or if head.Next is nil, in which case we return head itself.
Otherwise, we call ReverseListRecursively on head.Next, which returns the new head of the reversed linked list.
We then set head.Next.Next to head and head.Next to nil. Finally, we return the new head.
In the main function, we create a linked list 1 -> 2 -> 3 -> 4 -> 5, reverse it both iteratively and recursively,
and print out the reversed linked lists.
*/
package main
import "fmt"
// ListNode represents a node in the linked list
type ListNode struct {
Val int
Next *ListNode
}
// ReverseListIteratively reverses a linked list iteratively
func ReverseListIteratively(head *ListNode) *ListNode {
var prev, curr *ListNode
curr = head
for curr != nil {
nextTemp := curr.Next
curr.Next = prev
prev = curr
curr = nextTemp
}
return prev
}
// ReverseListRecursively reverses a linked list recursively
func ReverseListRecursively(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
newHead := ReverseListRecursively(head.Next)
head.Next.Next = head
head.Next = nil
return newHead
}
func main() {
// Create a linked list: 1 -> 2 -> 3 -> 4 -> 5
head := &ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{5, nil}}}}}
// Reverse the linked list iteratively
reversedListIteratively := ReverseListIteratively(head)
fmt.Println("Reversed linked list (iteratively):")
for reversedListIteratively != nil {
fmt.Printf("%d ", reversedListIteratively.Val)
reversedListIteratively = reversedListIteratively.Next
}
fmt.Println()
head = &ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{5, nil}}}}}
// Reverse the linked list recursively
reversedListRecursively := ReverseListRecursively(head)
fmt.Println("Reversed linked list (recursively):")
for reversedListRecursively != nil {
fmt.Printf("%d ", reversedListRecursively.Val)
reversedListRecursively = reversedListRecursively.Next
}
fmt.Println()
}