Skip to content

Commit 0a60cf3

Browse files
authored
Create solution.go
1 parent 44c946a commit 0a60cf3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

solution.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func reverseBetween(head *ListNode, left int, right int) *ListNode {
9+
if left == right {
10+
return head
11+
}
12+
dummy := &ListNode{
13+
Next: head,
14+
}
15+
var st *ListNode = dummy
16+
var idx int
17+
for idx < left-1 {
18+
st = st.Next
19+
idx++
20+
}
21+
var ed *ListNode = st.Next
22+
for idx < right-1 {
23+
tar := ed.Next
24+
ed.Next = tar.Next
25+
tar.Next = st.Next
26+
st.Next = tar
27+
idx++
28+
}
29+
return dummy.Next
30+
}

0 commit comments

Comments
 (0)