20200901: Reverse Linked List I & Reverse Linked List II (leetcode206&92) problem solutions (Java, C++, Python)

Reverse Linked List I & Reverse Linked List II (leetcode206&92) problem solutions (Java, C++, Python)

topic

  1. Inverted linked list Ⅰ
    Insert picture description here
    2. Inverted linked list Ⅱ
    Insert picture description here

Ideas and algorithms

  1. There are too many things to start school, and when I come back from work, I will start to do more today.
  2. The meaning of topic one is very simple, splicing the linked list, in actual operation, pay attention to the actual form of the linked list, NULL->1->2->3->4->5->NULL, our head directly points to 1 , So there is no previous process where NULL points to 1, and our countermeasures in actual operation are also very simple.
  3. Our purpose is clear: from 1->2->3->4->5->NULL to 5->4->3->2->1->NULL
  4. Pay attention to the operation of the process. 1 changes from no pre to pre to 2, 1 changes from suffix 2 to suffix to NULL, and can feel a bit of the taste of the predecessor. This question is really sad, the idea is very simple, **every time Your incumbent becomes the predecessor, and then you can choose the next one. At the end of the incumbency, the next one is in place, and then the incumbent becomes the predecessor. **If you say too much, it's tears. The trilingual problem is solved below. Next question
  5. The second topic is the first topic that restricts the reversal range. I won’t go into more details. The ideas are the same, but we need to pay attention to if our starting position is the first position, then the first position has no predecessor, that is, first love is irreplaceable. To understand this problem, I hope not to be too sad and cherish the present .

Code

  1. Reverse linked list Ⅰ
    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. Reverse linked list Ⅱ
    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    

Write at the end

Rush!

Guess you like

Origin blog.csdn.net/qq_36828395/article/details/108353696