[LeetCode-JAVA] Reverse Linked List I && II

题目I:

Reverse a singly linked list

思路:建立三个指针,一个用来维护链表头,另外两个再循环中,维护head的前一位和保存反转的后一位。

逐个反转,首先反转前两个,然后把前两个看成是一个,继续循环反转下去。

代码:

public class Solution {
    public ListNode reverseList(ListNode head) {
               ListNode dunmy = head;  //维护初始链表头,用于判断循环结束

        if(head == null || head.next == null)
            return head;
        ListNode pre = null;
        ListNode temp = null;
        while(dunmy.next != null){
            pre = head;              //记录当前节点
            head = dunmy.next;
            temp = head.next;   //保存next
            head.next = pre;
            dunmy.next = temp;
        }

        return head;
    }
}

题目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.

思路:在I的基础上,没有太大的变化,找到开始的位置后,进行反转。

需要注意的是建立一个虚拟链表头,这样在连接的时候比较方便。

代码:

public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode pre = new ListNode(0);  //虚拟链表头
        pre.next = head;
        ListNode dunmy = pre;
        int len = n-m;
        while(m > 1){  // 找到反转的位置
            head = head.next;
            pre = pre.next;
            m--;
        }
        pre.next = reverse(head, len);  //进行连接

        return dunmy.next;
    }
    public ListNode reverse(ListNode head, int n){
        ListNode dunmy = head;

        if(head == null || head.next == null)
            return head;
        ListNode pre = null;
        ListNode temp = null;
        while(n > 0){
            pre = head;
            head = dunmy.next;
            temp = head.next;
            head.next = pre;
            dunmy.next = temp;
            n--;
        }

        return head;
    }
}
时间: 2024-10-26 23:11:16

[LeetCode-JAVA] Reverse Linked List I && II的相关文章

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

【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->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 -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 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 follow

[Leetcode][JAVA] Pascal's Triangle I, II

Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe

Java for LeetCode 092 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 ≤ le

[LeetCode][JavaScript]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 cond

leetcode:142. Linked List Cycle II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50412899 题目地址:https://leetcode.com/problems/linked-list-cycle-ii/ Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return

LeetCode Solutions : 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 co

[LeetCode][Java] Reverse Nodes in k-Group

题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only