leetcode:92. Reverse Linked List II(Java)解答

原创 2015年12月26日 16:14:34

转载请注明出处:z_zhaojun的博客
原文地址:http://blog.csdn.net/u012975705/article/details/50408975
题目地址:https://leetcode.com/problems/reverse-linked-list-ii/

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->NULL, m = 2 and n = 4,

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

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

解法(Java):

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode listNode = null;
        while (head != null) {
            ListNode node = new ListNode(head.val);
            if (listNode != null) {
                node.next = listNode;
            }
            listNode = node;
            head = head.next;
        }
        return listNode;
    }

    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode lNode;
        ListNode mNode;
        ListNode rNode;
        lNode = head;
        boolean hasL = true;
        if (m == 1) {
            hasL = false;
        }
        n -= m;
        while (--m > 1) {
            head = head.next;
        }
        if (hasL) {
            mNode = head.next;
        } else {
            mNode = head;
        }
        if (mNode == null) {
            return lNode;
        }
        ListNode temp;
        temp = mNode;
        while (n-- > 0) {
            temp = temp.next;
        }
        rNode = temp.next;
        temp.next = null;
        mNode = reverseList(mNode);
        if (hasL) {
            head.next = mNode;
        } else {
            head = mNode;
        }
        while (mNode.next != null) {
            mNode = mNode.next;
        }
        mNode.next = rNode;
        if (hasL) {
            return lNode;
        }
        return head;
    }
}
版权声明:本文为博主原创文章,转载请注明出处。博客首页:http://blog.csdn.net/u012975705。订阅:http://blog.csdn.net/u012975705/rss/list

Reverse Linked List II -- LeetCode

原题链接: http://oj.leetcode.com/problems/reverse-linked-list-ii/  这道题是比较常见的链表反转操作,不过不是反转整个链表,而是从m到n的一部...

【LeetCode】Reverse Linked List II 解题报告

【题目】 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Giv...
  • ljiabin
  • ljiabin
  • 2014年12月05日 15:59
  • 4102

【LEETCODE】92-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...

LeetCode --- 92. Reverse Linked List II

题目链接:Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-p...
  • makuiyu
  • makuiyu
  • 2015年04月09日 10:50
  • 455

[Leetcode]#92 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-...

leetcode206/92---Reverse Linked List I/II(反转链表)

问题描述:Reverse a singly linked list. A linked list can be reversed either iteratively or recursively....
  • will130
  • will130
  • 2016年01月13日 15:09
  • 584

( Leetcode 92 ) Reverse Linked List II

题目:Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one...

LeetCode 92. Reverse Linked List II

1. 题目描述 Reverse a linked list from position m to n. Do it in-place and in one-pass. For examp...

leetcode No92. Reverse Linked List II

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

LeetCode 92. Reverse Linked List II ***** 虚拟头结点

题目 题意 注意 思路 代码题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:92. Reverse Linked List II(Java)解答
举报原因:
原因补充:

(最多只允许输入30个字)