Home >Backend Development >Golang >Reverse linked list in one row

Reverse linked list in one row

WBOY
WBOYforward
2024-02-09 09:18:191004browse

Reverse linked list in one row

php Xiaobian Yuzai introduces you to a common data structure algorithm - "Intra-row reverse linked list". In this algorithm, we need to reverse the order of nodes in a linked list. Through concise and efficient code implementation, we can complete this operation in one line, completely reversing the order of the linked list. This algorithm is very useful in actual programming and can play an important role in both data processing and algorithm design. Let’s learn about this wonderful algorithm together!

Question content

I just found the solution for reverse linked list using one line in go on leetcode. It does work, but I don't understand how to implement it.

That's it:

func reverselist(head *listnode) (prev *listnode) {
    for head != nil {
        prev, head, head.next = head, head.next, prev 
    }
    
    return
}

For example, let the list be [1->2->3->4->5->nil].

I know it works like this:

  1. First executehead.next = prev (head.next = nil, so nowhead = [1->nil] )

  2. Then, prev = head (In this step prev = [1->nil] just like head in the previous step )

  3. head = head.next This is magic. For prev in the second step of go, use head = [1->nil], but after this step head = [2->3-&gt ;4->5->nil]

So when head != nil it iterates and in the second step prev = [2->1->nil], head = [3->4->5->nil] and so on.

This line can be expressed as:

for head != nil {
        a := *head
        prev, a.Next = &a, prev
        head = head.Next
    }

Am I right? Why is this happening?

Solution

The variable on the left side of the expression will be assigned to the value on the right side of the expression at that time. This is a clever use of language.

To make it easier to understand, let’s look at an example.

set up

This is our link list: 1 -> 2 -> 3 -> 4 -> None

Before the function is executed,

  • The head is *node 1
  • prev is zero (uninitialized)
  • head.next is *node 2

Step by step

prev, head, head.next = head, head.next, prev

Let’s break it down,

  • prev (nil) = head (*node 1)
  • head (*Node 1) = head.next (*Node 2)
  • head.next (*node 2) = prev (nil)

Next iteration,

  • prev (*node 1) = head (*node 2)
  • head (*Node 2) = head.next (*Node 3)
  • head.next (*node 3) = prev (*node 1)

Summary

Basically, it reverses head.next to the previous node and moves prev and head to the next node.

Compare this to a textbook algorithm in go to be clear:

func reverseList(head *ListNode) *ListNode {
    var prev *ListNode

    for head != nil {
        nextTemp := head.Next
        head.Next = prev
        prev = head
        head = nextTemp
    }

    return prev
}

The above is the detailed content of Reverse linked list in one row. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete