LeetCode—206 Reverse Linked List Cpp&Python

LeetCode—206 Reverse Linked List Cpp&Python

Question requirement: Pass in the pointer of the linked list head and reverse the linked list

The process is as follows:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

1. Methods and ideas

Method 1: Iterative method
In the process of traversing the linked list, make the current node point to the previous node. In addition, another pointer is needed to store the next node and eventually return to the new head node.
The process is as follows

Insert picture description here
When traversing, complete three steps:
Step1: the
current node points to the previous node new_head
Step2:
new_head moves to the position of head
Step3:
head moves to the next position, in this process, you need to introduce the next node to save the position information to complete the operation


Method 2: Recursion
The idea of ​​recursion is to recurse to the end first, find the last node, and then start from the last node and turn the arrow direction.
Markup text
Recursive end conditions:

if(head == NULL || head->next == NULL) 
    return head;

Insert picture description here

After getting the last node, return to the upper layer of recursion, p points to the last node of the original linked list, and now it should be the head node, and the subsequent p does not need to be changed, and it continues to return to the upper layer of recursion

Change the head's next point to point to itself, and then point the head's next to empty, as the last node of the new linked list

head->next->next = head;
head->next = NULL;

Insert picture description here
The recursion ends, and the final return is p (head node)
Insert picture description here

Two, C++ code

#include <stdio.h>

struct ListNode {
    
    
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {
    
    }
};

class Solution {
    
    
public:
	ListNode* ReverseTheList1(ListNode* head){
    
    //迭代法
		ListNode *new_head = NULL;
		while (head)  //进行遍历
		{
    
    
			ListNode *next = head->next;
			head->next = new_head;
			new_head = head;
			head = next;
		}
		return new_head;
	}
	ListNode* ReverseTheList2(ListNode* head) {
    
    //递归法
		if (head == NULL || head->next == NULL)
			return head;

		ListNode* p = ReverseTheList2(head->next);
		head->next->next = head;
		head->next = NULL;
		return p;
	}
	
};
	int main() {
    
    //测试用例
		ListNode a1(1);ListNode b1(2);ListNode c1(3);ListNode d1(4);ListNode e1(5);
		a1.next = &b1; b1.next = &c1; c1.next = &d1; d1.next = &e1;
		ListNode a2(1); ListNode b2(2); ListNode c2(3); ListNode d2(4); ListNode e2(5);
		a2.next = &b2; b2.next = &c2; c2.next = &d2; d2.next = &e2;
		Solution solve;
		ListNode *head1 = solve.ReverseTheList1(&a1);
		ListNode *head2 = solve.ReverseTheList2(&a2);
		while (head1){
    
    
			printf("%d\n", head1->val);
			head1 = head1->next;
		}
		printf("------------\n");
		while (head2) {
    
    
			printf("%d\n", head2->val);
			head2 = head2->next;
		}
		return 0;
	}
	//Time:O(n) Sapce:O(1)

Three, Python code

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

class Solution:
	#迭代法
    def reverseList1(self, head: ListNode) -> ListNode:
        pre, cur = None, head
        while cur:
            temp = cur.next
            cur.next = pre
            pre, cur = cur, temp
        return pre

	#递归法
     def reverseList(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        newhead = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return newhead

Guess you like

Origin blog.csdn.net/weixin_45680994/article/details/108531231