[Linked list problem] How to elegantly reverse a singly linked list

Preface

Update the topic-brushing posts in the form of special topics. Welcome to study topic-brushing with me. Trust me, your persistence will definitely bring unexpected gains. Each question will provide a simple answer, if you have a more elegant approach, welcome to provide guidance, thank you

[Topic description]

Reverse singly linked list. For example, the linked list is:

1->2->3->4

After inversion

4->3->2->1

【Claim】

If the length of the linked list is N, the time complexity reaches O(N), and the extra space complexity reaches O(1)

【Difficulty】

Taxi: ★☆☆☆

【answer】

method 1

This question is quite simple. When we are reversing a node, just change the rear drive of a node to point to its predecessor. The point to note here is that when you point the back of the current node to the front, the linked list will be truncated at this time, that is to say, the back node is separated from the current node, so we need a variable to save the back of the current node Drive, visit lost.

The specific code is as follows:

code show as below

1//节点
2class Node{
3    public int value;
4    public Node next;
5    public Node(int data) {
6        this.value = data;
7    }
8}

Main code

1//Reverse singly linked list 2
public static Node reverseList(Node head) {
3 Node next = null;//Point to the back of the current node
4 Node pre = null;//Point to the predecessor of the current node
5 while (head != null ) {
6 next = head.next;
7 //The back drive of the current node points to the predecessor
8 head.next = pre;
9 pre = head;
10 //Process the next node
11 head = next;
12}
13 return pre;

Method Two

This problem can also be done with recursion, assuming that the function of the reverse() method is to reverse the singly linked list. When using the recursive method, we can continue to recurse the child linked list. For example, for the following linked list:

[Linked list problem] How to elegantly reverse a singly linked list
We recurse the child linked list 2->3->4, that is,
Node newList = reverse(head.next). The result after recursion is as follows:
[Linked list problem] How to elegantly reverse a singly linked list

After the reversal, the child list 2->3-> becomes 4->3->2.
Note that I just assumed that the function of reverse() is to reverse the linked list. But at this time, node 1 is still pointing to node 2. At this time, we reverse the nodes 1 and 2 again, and then the next node of 1 points to null. As shown:
[Linked list problem] How to elegantly reverse a singly linked list

The end condition of recursion is: when the child linked list has only one node or is null, the recursion ends. code show as below:

 1    //用递归的方法反转链表
 2    public static Node reverseList2(Node head){
 3        if (head == null || head.next == null) {
 4            return head;
 5        }
 6        //递归反转子lian链表
 7        Node newList = reverseList2(head.next);
 8        //第三张图
 9        head.next.next = head;
10        head.next = null;
11        return newList;
12    }

Problem expansion

Topic: Reverse part of the linked list node

[Topic description]

Topic: Given the head node head of a singly linked list, and two integers from and to, reverse the from node and to node on the single chain list

列如:
1->2->3->4->5->null,from=2,to=4

Result: 1->4->3->2->5->null

Listed as:

1->2->3->null from=1,to=3

The result is 3->2->1->null

【Claim】

1. If the length of the linked list is N, the time complexity requirement is O(N), and the additional space complexity requirement is O(1)

2. If 1<=from<=to<=N is not satisfied, no adjustment

【Difficulty】

Taxi: ★☆☆☆

【answer】

You can do it yourself or think about it. If you want to get the answer, you can reply to answer 2 on the official account to get the code

  • End -

    Past

[Linked list problem] Delete the intermediate node of a singly linked list

[Linked list problem] Delete the Kth node in the singly linked list

[Algorithm combat] Generate window maximum array

[Linked list problem] How to elegantly reverse a singly linked list

Guess you like

Origin blog.51cto.com/15015171/2555346