LeetCode 92 反转链表 II


反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。

说明:
1 ≤ m ≤ n ≤ 链表长度。

示例:

输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL


5ms 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
		 if(m==n){
            return head;
        }
        m = m-1;
		n = n-1;
        
		List<ListNode> list = new ArrayList();
        ListNode cur = head;
        while(cur!=null){
            ListNode tmp = cur.next;
            cur.next = null;
            list.add(cur);
            cur = tmp;
        }
		ListNode a;
		ListNode b;
		while(m<n){
            a = list.get(m);
		    b = list.get(n);
		    list.set(m,b);
		    list.set(n,a);
            m++;
            n--;
        }
		
		ListNode newHead = list.get(0);
		ListNode next;
		for(int i=0;i<list.size()-1;i++){
			next = list.get(i);
			next.next = list.get(i+1);
		}
		return  newHead;
	}
}


优化4ms

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
     public ListNode reverseBetween(ListNode head, int m, int n){
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        ListNode cur = newHead;
        for(int i=0;i<m-1;i++){
            cur = cur.next;
        }
        ListNode first = cur;
        ListNode last = cur.next;
        ListNode pre = null;
        cur = cur.next;
        ListNode tmp;
        for(int i=0;i<n-m+1;i++){
            tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        first.next = pre;
        last.next = cur;
        return newHead.next;
    }
}

优化3ms

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if (head == null || head.next == null) {
            return head;
        }
        
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        ListNode prev = dummy;
        for (int i = 0; i < m - 1; i++) {
            prev = prev.next;
        }
        ListNode start = prev.next;
        ListNode then = start.next;
        
        for (int i = 0; i < n - m; i++) {
            start.next = then.next;
            then.next = prev.next;
            prev.next = then;
            then = start.next;
        }
        
        return dummy.next;
    }
}


给定一个值3,需要对数组[1,2,3,4,5] 反转, 需要得到结果 [3,2,1,5,4]

public ListNode reversetTwo(ListNode head,int m,int total){
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode start = pre.next;
        ListNode then = start.next;
        for(int i=0;i<m-1;i++){
            start.next = then.next;
            then.next = pre.next;
            pre.next = then;
            then = start.next;
        }
        int n = total-m;
        pre = start;
        start = start.next;
        then = then.next;
        for(int i=0;i<n-1;i++){
            start.next = then.next;
            then.next = pre.next;
            pre.next = then;
            then = start.next;
        }
        return dummy.next;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值