[leedcode 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->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.

/**
 * 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) {
        /*The idea is stright forward. There needs an safe head guard.

        1 calculate the number that needs to reverse 

        2 find the start position to reverse.

        3. iterative reverse two node(Use two pointer)*/
        ListNode newHead=new ListNode(-1);//安全头节点
        newHead.next=head;
        ListNode p1=head;
        ListNode last=newHead;
        int t=n-m;
        while(m>1){
            p1=p1.next;
            last=last.next;
            m--;
        }
        ListNode rear=last;
        ListNode temp=p1;
            p1=p1.next;
            last=last.next;
        ListNode p2=p1;

        while(t>0){
            p2=p1.next;
            p1.next=last;
            last=p1;
            p1=p2;
            t--;
        }
        rear.next=last;
        temp.next=p1;
        return newHead.next;
    }
}
时间: 2024-11-05 18:41:16

[leedcode 92] Reverse Linked List II的相关文章

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->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 ≤ lengt

LeetCode OJ 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->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 ≤ lengt

92. Reverse Linked List II (List)

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 ≤ le

【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->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 ≤ lengt

leetCode 92.Reverse Linked List II (反转链表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 ≤ le

[LeetCode] 92. Reverse Linked List II Java

题目: 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 ≤ l

92. Reverse Linked List II - Medium

Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 只反转从m到n的部分链表 M1: iterative 首先找到prev的位置(开始反转位置的前一个节

LeetCode 92. Reverse Linked List II

将链表分成三段,中间一段reverse后再链接. 链表中节点位置从1开始计数. 注意m可能为1,所以要加上if(last == start) head = end;    不然会丢失数据,因为reverse后head是reverse一段的最后一节点,只返回head的话head之前的节点全部丢失. 4ms 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *nex

【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List

The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three pointers to achieve this goal. 1) Pointer to last value 2) Pointer to cur p value 3) Pointer to next value Here, showing my code wishes can help u. #incl