How to reverse the linked list by brushing the leetcode linked list with golang
Editor to share with you golang brush leetcode linked list how to reverse the linked list, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Reverses the linked list from position m to n. Please use a scan to complete the reversal.
Description:
1 ≤ m ≤ n ≤ linked list length.
Example:
Enter: 1-> 2-> 3-> 4-> 5-> NULL, m = 2, n = 4
Output: 1-> 4-> 3-> 2-> 5-> NULL
Ideas for solving the problem:
Find the starting position, record the position before and after the rotation, rotate the intermediate element, pay attention to the boundary
/ * Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode *} * / func reverseBetween (head * ListNode, m int, n int) * ListNode {if head==nil | | head.Next==nil {return head} he:=&ListNode {} he.Next=head cur:=he for iPart 0 ListNode i3-> 2-> 5-> 2, x = 3
Output: 1-> 2-> 2-> 4-> 3-> 5 Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode *} * / func partition (head * ListNode, x int) * ListNode {if head==nil {return nil} var H2 ListNode T2 * ListNode for headquarternil {if head.Val