题解 | #链表内指定区间反转#
链表内指定区间反转
http://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
function ListNode(x){ this.val = x; this.next = null; }
/** *
- @param head ListNode类
- @param m int整型
- @param n int整型
- @return ListNode类 */ function reverseBetween( head , m , n ) { // write code here let startFan=new ListNode(undefined); startFan.next=head; let ans=startFan; for(let i=1;i<m;++i){ startFan=startFan.next; } let a=startFan,b,endb; for(let i=0;i<=n-m;++i){ startFan=startFan.next; let Vnode=new ListNode(startFan.val); if(i==0){ endb=Vnode; } Vnode.next=b; b=Vnode; } a.next=b; if(startFan.next) endb.next=startFan.next; return ans.next; } module.exports = { reverseBetween : reverseBetween };