Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to reverse the linked list by brushing the leetcode linked list with golang

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report