package main import "fmt" import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ var stack [] *ListNode func push(val *ListNode){ stack =append(stack,val) } func reverse(start int,end int){ for i,j:=start,end;i<j;i,j=i+1,j-1{ stack[i],stack[j]=stack[j],stack[i] } } func reverseBetween( head *ListNode , m int , n int ) *ListNode { // write code here if(head == nil || head.Next == nil){ return head } for head != nil{ push(head) head=head.Next } reverse(m-1,n-1) newHead:=&ListNode{} resHead:=newHead for i:=0;i<len(stack);i++{ newHead.Next = &ListNode{Val: stack[i].Val} newHead=newHead.Next fmt.Println(stack[i]) } return resHead.Next }