Reverse Linked List II

题目:

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

 1     public ListNode reverseBetween(ListNode head, int m, int n) {
 2         if(head == null) return null;
 3         ListNode dummy = new ListNode(0);
 4         dummy.next = head;
 5         ListNode pre = dummy;
 6         ListNode start = head;
 7         
 8         for (int i = 1; i < m; i++) {
 9             start = start.next;
10             pre = pre.next;
11         }
12         ListNode end = start;
13         ListNode then = start.next;
14         ListNode temp = then;
15         for (int j = 0; j < n - m;  j++) {
16             temp = then.next;
17             then.next = start;
18             start = then;
19             then = temp;
20         }
21         pre.next = start;
22         end.next = then;
23         return dummy.next;
24     }

感觉写的挺丑的。。。

转载于:https://www.cnblogs.com/gonuts/p/4418880.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值