LeetCode 206: reverse list Reverse Linked List

Reverse a singly linked list.

Reverse a singly linked list.

Example:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

Advanced:
You can reverse iterative or recursively list. Can you solve this question in two ways?

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

Problem-solving ideas:

Each traverse to the last node to take this approach even if the time complexity is too high. Advanced topics such as the requirements of the two methods, iteration and recursion:

Iteration:

Each time a node out of the sub-node as the primary node is added to the new list:

Original list: 1-> 2-> 3-> 4-> 5

Separating the first node to the new node is added as header list: the list of the original 1: 2-> 3-> 4-> 5

The separated head node as a node is added to the new list: 2-> 1 original list: 3-> 4-> 5

The separated head node as a node is added to the new list: 3-> 2-> 1 original list: 4-> 5

The separated head node as a node is added to the new list: 4-> 3-> 2-> 1 original list: 5

The separated head node as a node is added to the new list: 5-> 4-> 3-> 2-> 1 original list: null

Java:

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode pre = null;
        ListNode tmp = null;
        while (head != null) {
            tmp = head.next;//tmp暂存当前节点的下一个节点
            head.next = pre;//当前节点下一个指向pre
            pre = head;//刷新pre
            head = tmp;//刷新当前节点为tmp
        }
        return pre;
    }
}

Python3:

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        pre,tmp=None,None
        while(head):
            tmp=head.next
            head.next=pre
            pre=head
            head=tmp
        return pre

Recursion:

It is actually a complete stack of recursive functions: last out

Baseline conditions encountered an empty node (to the end of the list), returns the object for the last node of the list, passing in a recursive function has been the same. Separated one by one node and a new node is added to the end of the list to the head from the end of the list. And iterative methods similar principle.

Original list: 1-> 2-> 3-> 4-> 5

Recursive the final layer encountered node returns null tail node 5

Recurrent back layer 5 as the tail node of the split node of the new list: 5, 5 Node original blank, the original list 1-> 2-> 3-> 4-> null

Recurrent back layer 4 as the tail node of the split node of the new list: 5-> 4, blanking original node 4, the original list 1-> 2-> 3-> null

Recurrent back to the previous node 3 as a new separate tail node list: 5-> 4-> 3, blanking original node 3, the original list 1-> 2-> null

Recurrent back to the previous node of the split node 2 as a new linked list: 5-> 4-> 3-> 2, original blanking node 2, the original list 1-> null

Recurrent back to the previous node of the split node as a new linked list: 5-> 4-> 3-> 2-> 1, blanking the original node, the original null list

Java:

class Solution {
    public ListNode reverseList(ListNode head) {
        //基线条件
        if (head == null || head.next == null) return head;
        //递归
        ListNode tmp = head.next;//暂存头节点的下一个节点
        ListNode pre = reverseList(head.next);//递归调用该函数,pre为返回的新链表的头节点,原链表的最后一个节点,无论递归多少层该返回值一直传递不变
        tmp.next = head;//暂存的下一个节点指向传入节点
        head.next = null;//下一个节点即原本指向tmp的节点 置空
        return pre;
    }
}

Python3:

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        tmp = head.next
        pre = self.reverseList(head.next)
        tmp.next = head
        head.next = None
        return pre

Welcome to public attention with the brush No. title: Write Love Bug

I love to write Bug.png

Guess you like

Origin www.cnblogs.com/zhangzhe532/p/11224399.html