经典指数          
原因
945
浏览数
0
收藏数
 

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given1->2->3->4->5->NULL, m = 2 and n = 4, return1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.

     举报   纠错  
 
切换
1 个答案
要定义一个链表头结点的指针,因为有可能反转的第一个节点是head; 不能采用从发转的第一个元素开始向后插入的方式,否则当要发转的元素个数为奇数时,{1,2,3,4,5},2,4;输出结果会是{1,4,2,3,5} public class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { if(head==null) return null; int diff=n-m; ListNode headPre=new ListNode(0); headPre.next=head; ListNode slow=head; ListNode slowPre=headPre; for(int i=1;i
 
切换
撰写答案
扫描后移动端查看本题