当前位置:网站首页>Leetcode question brushing series - mode 2 (datastructure linked list) - 206:reverse linked list

Leetcode question brushing series - mode 2 (datastructure linked list) - 206:reverse linked list

2022-06-11 04:37:00 Dabie Mountains

leetcode Brush questions series ---- Pattern 2(Datastructure Linked list )- 206:Reverse Linked List Reverse a linked list

Tips

  • For more information, please refer to the catalogue of this series
  • This problem can be solved by iteration .
  • First define current and previous Two pointers .
  • Use head preservation current Of next node .
  • To break off current and current Of next The chain between .
  • establish current and current Of next New link between ,current Of next Change to point previous.
  • previous Be released , take current Turn into previous,head assignment current.
  • Start a new iteration , Thought seems to have the shadow of a double pointer .

Python

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        current = head
        previous =  None
        while current:
            temp = current.next
            current.next = previous
            previous = current 
            current = temp
        return previous

C++

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
    
public:
    ListNode* reverseList(ListNode* head) {
    
        ListNode* current = head;
        ListNode* previous = nullptr;
        //  here head It's useless , Use head As caching 
        while(current!=nullptr)
        {
    
            head = current->next;
            current->next = previous;
            previous = current;
            current = head;
            
        }
        return previous;
    }
};

C#

/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */
public class Solution {
    
    public ListNode ReverseList(ListNode head) {
    
        ListNode current = head;
        ListNode previous = null;
        while(current!=null)
        {
    
            head = current.next;
            current.next = previous;
            previous = current;
            current = head;
        }
        return previous;
    }
}

Java

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
    
    public ListNode reverseList(ListNode head) {
    
        ListNode current = head;
        ListNode previous = null;
        while(current!=null)
        {
    
            head = current.next;
            current.next = previous;
            previous = current;
            current = head;
        }
        return previous;
    }
}
原网站

版权声明
本文为[Dabie Mountains]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020548165009.html