20200901:反转链表Ⅰ&反转链表Ⅱ(leetcode206&92)题解(Java,C++,Python)

反转链表Ⅰ&反转链表Ⅱ(leetcode206&92)题解(Java,C++,Python)

题目

  1. 反转链表Ⅰ
    在这里插入图片描述
    2.反转链表Ⅱ
    在这里插入图片描述

思路与算法

  1. 开学事太多,忙完回来,今天开始更,再费劲也更
  2. 题目一的意思很简单,拼接链表,实际操作中就注意链表的实际形态是这个样子,NULL->1->2->3->4->5->NULL,我们的head直接指向了1,因此没有前面那个NULL指向1的过程,实际操作中我们的反制也很简单。
  3. 我们的目的明确:从1->2->3->4->5->NULL5->4->3->2->1->NULL
  4. 注意过程的操作,1从没pre到pre变为2,1从有后缀2到后缀变成NULL,又能体会出点前任的味道,本题属实令人难过,思路很简单,**每次将你的现任变前任,再将下一任选出来,在现任结束时,下一任到位,然后现任变前任。**说多了都是泪,三语题解下列。下一问
  5. 题目二就是限制了反转区间的题目一,不多赘述,思路一致,只是需要注意我们的起始位置如果为第一个位置,那么第一个位置没有前任,也就是初恋是不可替代的这种问题的理解,希望不要太难过,珍惜当下

代码实现

  1. 反转链表Ⅰ
    Java:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public ListNode reverseList(ListNode head) {
    
    
        ListNode first = head;
        ListNode new_head = null;
        while (first != null) {
    
    
            ListNode next = first.next;
            first.next = new_head;
            new_head = first;
            first = next;
        }
        return new_head;
    }
}

C++:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* reverseList(ListNode* head) {
    
    
        ListNode *new_head = NULL;
        while (head) {
    
    
            ListNode* next = head->next;
            head->next = new_head;
            new_head = head;
            head = next;
        }
        return new_head;
    }
};

Python:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        pre, cur = None, head
        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre
  1. 反转链表Ⅱ
    Java:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public ListNode reverseBetween(ListNode head, int m, int n) {
    
    
        int change_len = n - m + 1;
        ListNode pre_head = null;
        ListNode result = head;
        
        while ((head != null) && (--m != 0)) {
    
    
            pre_head = head;
            head = head.next;
        }
        ListNode list_tail = head;
        ListNode new_head = null;
        while (change_len != 0 && (head != null)) {
    
    
            ListNode next = head.next;
            head.next = new_head;
            new_head = head;
            head = next;
            change_len--;
        }
        list_tail.next = head;
        if (pre_head != null) {
    
    
            pre_head.next = new_head;
        } else {
    
    
            result = new_head;
        }
        return result;
    }
}

C++:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
    
    
        // 要反转的长度
        int change_len = n - m + 1;
        ListNode *pre_head = NULL;
        ListNode *result = head;
        // 向后移动head m-1次即可找到那个 等待反转的子list的头结点。
        while (head && --m) {
    
    
            pre_head = head;
            head = head->next;
        }

        ListNode *list_tail = head;
        ListNode *new_head = NULL;
        // 从找到的子list的head开始进行翻转,这个和之前的反转链表1是一样的,不做赘述,加一个计数即可
        while (head && change_len) {
    
     
            ListNode *next = head->next;
            head->next = new_head;
            new_head = head;
            head = next;
            change_len--;
        }
        // 此时head已经移动到子list尾巴的后一位,因此进行连接即可
        list_tail->next = head;
        // 进行连接,连接的时候区分如果m=1的时候,没有pre_head的特殊情况。
        if (pre_head){
    
    
            pre_head->next = new_head;
        } else {
    
    
            result = new_head;
        }
        // 返回最后的头结点
        return result;
    }
};

Python:

class Solution:
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
        # python中特况的处理比较易写,语法简单,写法有稍许变动
        if m == 1: return self.reverse(head, m, n)
        count = 1
        h = head
        while h:    
            if count == m - 1:
                pre_head = h 
            h = h.next
            count += 1
        new_head = self.reverse(pre_head.next, m, n)
        # 连接头,尾连接写在reverse里了
        pre_head.next = new_head 
        return head
    '''
    子函数实现对当前head开始翻转n-m+1范围,并连接尾
    '''
    def reverse(self, head, m, n):
        pre, cur = None, head
        for _ in range(n - m + 1):
            pre, pre.next, cur = cur, pre, cur.next
        head.next = cur 
        return pre    

写在最后

冲!

猜你喜欢

转载自blog.csdn.net/qq_36828395/article/details/108353696