Jan 23 - Reverse Linked List; Linked List; Pointers;

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jan 23 - Reverse Linked List; Linked List; Pointers;相关的知识,希望对你有一定的参考价值。

/**
 * 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) {
        ListNode cur = head;
        ListNode prev = null;
        int i = 1;
        while(i < m){
            prev = cur;
            cur = cur.next;
            i++;
        }
        ListNode start = cur, end = cur;
        while(i < n){
            ListNode nex = end.next.next;
            end.next.next = start;
            start = end.next;
            end.next = nex;
            cur = nex;
            i++;
        }
        if(prev != null){
            prev.next = start;
            return head;
        }
        return start;
    }
}

Too late night now, no energy to say anything...Just pointer operation.

以上是关于Jan 23 - Reverse Linked List; Linked List; Pointers;的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode Reverse Linked List

Leetcode 206. Reverse Linked List

Leetcode92. Reverse Linked List II && 206. Reverse Linked List

Jan 29 - Flatten Binary Tree To Linked List; DFS;

206. Reverse Linked List

链表-Reverse Linked List