leetcode Reverse Linked List II

原创 2016年06月01日 22:06:25

题目链接

这个题目看上去很简单。但是其实考虑的问题有很多。。最特殊情况。如果从第一个就开始反转,那么链表头结点要改

一个简单的方法就是增加一个头结点。这样省着改头结点

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

        ListNode current=null;
        ListNode nextNode=null;

        ListNode workHead=null;
        ListNode workTail=null;

        if(m!=1)
        {
            current=front.next;
            nextNode=current.next;
            current.next=null;
            workHead=current;
            workTail=current;
        }
        else
        {
            current=head;
            nextNode=head.next;
            head.next=null;
            workHead=head;
            workTail=head;
        }

        for(int i=1;i<n-m+1;i++)
        {
            current=nextNode;
            nextNode=current.next;
            current.next=workHead;
            workHead=current;
        }
        workTail.next=nextNode;
        if(m!=1)
        {
        front.next=workHead;
        }
        if(m==1)
        {
            return workHead;
        }
        return head;

    }
}

大神做法

ListNode* reverseBetween(ListNode *head, int m, int n) {
    ListNode dummy(0), *prev = &dummy;
    dummy.next = head;
    for (int i=1; i<m; i++)
        prev = prev->next;
    ListNode *pivot = prev->next;
    for (int i=m; i<n; i++) {
        swap(prev->next, pivot->next->next);
        swap(prev->next, pivot->next);
    }
    return dummy.next;
}

相关文章推荐

( 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 Reverse Linked List II

题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Giv...

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] 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...
  • game115
  • game115
  • 2014年04月17日 23:29
  • 703

leetcode -day30 Reverse Linked List II

1、  Reverse Linked List II  Reverse a linked list from position m to n. Do it in-place and in...

Reverse Linked List II leetcode

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

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】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...

[LeetCode] Reverse Linked List II Solution

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 a linked list from position m to n. Do it in-place and in one-pass. ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Reverse Linked List II
举报原因:
原因补充:

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