LeetCode 24 reverse list

24. Reverse linked list

The glory of the king who played a day today did not study well ... it really should not be

Define a function, input the head node of a linked list, reverse the linked list and output the head node of the inverted list.

Example:

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

Limits :

0 <= number of nodes <= 5000

Use of external space

Idea: This method is very simple, first apply for a dynamically expanded array or container, such as ArrayList. Then continue to traverse the linked list, add the elements of the linked list to this container. Then use the container's own API to reverse the entire container, so that the reverse effect is achieved. Finally traverse the container and the linked list at the same time, and change the value in the linked list to the value in the container. Because the value of the container at this time is: the
5 4 3 2 1
linked list is reset to one side in this order, and it meets the requirements.
Of course, you can create N new nodes, and then return, which can also achieve the purpose.
This method is very simple, but if you do this in an interview, the interviewer will 100% ask whether there is a better way, such as not using external space. So I will not do the code and animation demonstration, let's take a look at how to use O (1) O (1) space complexity to achieve this problem.

Result: The space complexity is a bit high

// ...........
Double pointer iteration

Idea: We can apply for two pointers. The first pointer is called pre, which initially points to null. The second pointer, cur, points to head, and then continuously traverses cur. Every time iterates to cur, it points the next of cur to pre, and then pre and cur advance one bit. After all iterations (cur becomes null), pre is the last node.

result:

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    let pre = null, cur = head, temp = null
    while(cur != null) {
        temp = cur.next
        cur.next = pre
        pre = cur
        cur = temp
    }
    return pre
};
//  吓死我了 提交第一次 108ms 第二次80ms 第三次64ms 第四次就打败了95.86%的用户了

Next, try recursive writing

Recursive solution

This question has a very irritating recursive solution. The recursive solution is very difficult to understand. It is best to understand it with code and animation.
Two conditions for recursion:

  1. The termination condition is the current node or the next node == null
  2. Inside the function, change the point of the node, that is, the next node of head points to the head recursive function

It's hard to understand, in fact, the next node of head points to head. The cur returned by each recursive function is actually only the last node. Inside the recursive function, what changes is the direction of the current node.

var reverseList = function(head) {
 if (head === null || head.next === null) {
     return head
 }
 let newReversedList = reverseList(head.next)
 head.next.next = head
 head.next = null
 return newReversedList
};

Guess you like

Origin www.cnblogs.com/ssaylo/p/12729603.html