当前位置: 首页 > wzjs >正文

新乡专业网站制作网络优化器

新乡专业网站制作,网络优化器,wordpress文字中间,如何通过建设一个网站赚钱1. 题目 描述 给你单链表的头指针 head 和两个整数 left 和 right &#xff0c;其中 left < right 。请你反转从位置 left 到位置 right 的链表节点&#xff0c;返回 反转后的链表 。 示例1 输入&#xff1a; 输入&#xff1a;head [1,2,3,4,5], left 2, right 4 输…

1. 题目

描述

给你单链表的头指针 head 和两个整数 leftright ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表

示例1

输入:

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]

示例 2

输入:head = [5], left = 1, right = 1
输出:[5]

提示:

  • 链表中节点数目为 n

  • 1 <= n <= 500

  • -500 <= Node.val <= 500

  • 1 <= left <= right <= n

2. 解题思路

第1步:定义一个临时链表头节点。

第2步:定义(找到)截取区间外的指针变量:pre、post,截取区间指针变量:left、right。

第3步:切断原连接。

第4步:翻转局部链表。

链表的局部反转,可以参考上一篇讲解的《可视化图解算法:反转链表》。

第5步:接回原来的链表。

如果文字描述的不太清楚,你可以参考视频的详细讲解。

  • Python编码:哔哩哔哩_bilibilihttps://www.bilibili.com/cheese/play/ep1370258

  • Java编码:哔哩哔哩_bilibilihttps://www.bilibili.com/cheese/play/ep1366714

  • Golang编码:数据结构笔试面试算法-Go语言版_哔哩哔哩_bilibili数据结构笔试面试算法-Go语言版,bilibili课堂,哔哩哔哩课堂,哔哩哔哩,Bilibili,B站,弹幕https://www.bilibili.com/cheese/play/ep1364391

3. 编码实现

3.1 Python编码实现

class ListNode:def __init__(self, x):self.val = x  # 链表的数值域self.next = None  # 链表的指针域# 从链表节点尾部添加节点
def insert_node(node, value):if node is None:print("node is None")return# 创建一个新节点new_node = ListNode(value)cur = node# 找到链表的末尾节点while cur.next is not None:cur = cur.next# 末尾节点的next指针域连接新节点cur.next = new_node# 打印链表(从链表头结点开始打印链表的值)
def print_node(node):cur = node# 遍历每一个节点while cur is not None:print(cur.val, end="\t")cur = cur.next  # 更改指针变量的指向print()def reverseList(left):if left is None:returnpre = None  # (操作的)前序节点cur = left  # (操作的)当前节点nxt = left  # (操作的)下一个节点while cur is not None:nxt = cur.nextcur.next = prepre = curcur = nxt#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @param m int整型
# @param n int整型
# @return ListNode类
#
class Solution:def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:# write code here# 1. 定义一个临时链表头节点tmp_head = ListNode(-1)tmp_head.next = head# 2.定义(找到)截取区间外的指针变量:pre、post,截取区间指针变量:left、rightpre = tmp_headfor i in range(m - 1):pre = pre.nextleft = pre.nextright = tmp_headfor i in range(n):right = right.nextpost = right.next# 3.切断链接pre.next = Noneright.next = None# 4. 翻转局部链表reverseList(left)# 5.接回原来的链表pre.next = rightleft.next = postreturn tmp_head.nextif __name__ == '__main__':root = ListNode(1)insert_node(root, 2)insert_node(root, 3)insert_node(root, 4)insert_node(root, 5)print_node(root)s = Solution()head = s.reverseBetween(root, 2, 4)print_node(head)

3.2 Java编码实现

package LL02;import javax.swing.plaf.nimbus.NimbusLookAndFeel;public class Main {//定义链表节点static class ListNode {private int val;  //链表的数值域private ListNode next; //链表的指针域public ListNode(int data) {this.val = data;this.next = null;}}//添加链表节点private static void insertNode(ListNode node, int data) {if (node == null) {return;}//创建一个新节点ListNode newNode = new ListNode(data);ListNode cur = node;//找到链表的末尾节点while (cur.next != null) {cur = cur.next;}//末尾节点的next指针域连接新节点cur.next = newNode;}//打印链表(从头节点开始打印链表的每一个节点)private static void printNode(ListNode node) {ListNode cur = node;//遍历每一个节点while (cur != null) {System.out.print(cur.val + "\t");cur = cur.next; //更改指针变量的指向}System.out.println();}public static class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** @param head ListNode类* @param m    int整型* @param n    int整型* @return ListNode类*/public ListNode reverseBetween(ListNode head, int m, int n) {// write code here//1. 定义一个临时链表头节点ListNode tmpHead = new ListNode(-1);tmpHead.next = head;//2.定义(找到)截取区间外的指针变量:pre、post,截取区间指针变量:left、rightListNode pre = tmpHead;for (int i = 0; i < m - 1; i++) {pre = pre.next;}ListNode left = pre.next;ListNode right = tmpHead;for (int i = 0; i < n; i++) {right = right.next;}ListNode post = right.next;//3.切断链接pre.next = null;right.next = null;//4.反转局部链表reverseList(left);//5.接回原来的链表pre.next = right;left.next = post;return tmpHead.next;}private void reverseList(ListNode left) {ListNode pre = null; //前序节点ListNode cur = left; //(操作的)当前节点ListNode nxt = left;//(操作的)下一个节点while (cur != null) {nxt = cur.next;cur.next = pre;pre = cur;cur = nxt;}}}public static void main(String[] args) {ListNode root = new ListNode(1);insertNode(root, 2);insertNode(root, 3);insertNode(root, 4);insertNode(root, 5);printNode(root);Solution solution = new Solution();ListNode head = solution.reverseBetween(root, 2, 4);printNode(head);}
}

3.3 Golang编码实现

package mainimport ("fmt"
)type ListNode struct {Val  int       //链表的数值域Next *ListNode //链表的指针域
}/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param head ListNode类* @param m int整型* @param n int整型* @return ListNode类*/
func reverseBetween(head *ListNode, m int, n int) *ListNode {// write code here//1. 定义一个临时链表头节点tempHead := &ListNode{Val: -1}tempHead.Next = head//2.定义(找到)截取区间外的指针变量:pre、post,截取区间指针变量:left、rightpre := tempHeadfor i := 0; i < m-1; i++ {pre = pre.Next}left := pre.Nextright := tempHeadfor i := 0; i < n; i++ {right = right.Next}post := right.Next//3.切断链接pre.Next = nilright.Next = nil//4.翻转局部链表reverseList(left)//5.接回原来的链表pre.Next = rightleft.Next = postreturn tempHead.Next
}func reverseList(left *ListNode) {var pre *ListNode //(操作的)前序节点cur := left       //(操作的)当前节点nxt := left       //(操作的)下一个节点for cur != nil {nxt = cur.Nextcur.Next = prepre = curcur = nxt}
}
func main() {root := &ListNode{Val: 1}root.Insert(2)root.Insert(3)root.Insert(4)root.Insert(5)root.Print()node := reverseBetween(root, 2, 4)node.Print()
}// Insert 从链表节点尾部添加节点
func (ln *ListNode) Insert(val int) {if ln == nil {return}//创建一个新节点newNode := &ListNode{Val: val}cur := ln//找到链表的末尾节点for cur.Next != nil {cur = cur.Next}//末尾节点的next指针域连接新节点cur.Next = newNode
}// Print 从链表头结点开始打印链表的值
func (ln *ListNode) Print() {if ln == nil {return}cur := ln//遍历每一个节点for cur != nil {fmt.Print(cur.Val, "\t")cur = cur.Next //更改指针变量的指向}fmt.Println()
}

如果上面的代码理解的不是很清楚,你可以参考视频的详细讲解。

  • Python编码:哔哩哔哩_bilibilihttps://www.bilibili.com/cheese/play/ep1370258

  • Java编码:哔哩哔哩_bilibilihttps://www.bilibili.com/cheese/play/ep1366714

  • Golang编码:数据结构笔试面试算法-Go语言版_哔哩哔哩_bilibili数据结构笔试面试算法-Go语言版,bilibili课堂,哔哩哔哩课堂,哔哩哔哩,Bilibili,B站,弹幕https://www.bilibili.com/cheese/play/ep1364391

4.小结

对于链表内指定区间反转的反转,可以通过5步操作完成:

(1)定义一个临时链表头节点;(2)定义(找到)截取区间外的指针变量:pre、post,截取区间指针变量:left、right;(3)切断链接;(4)翻转局部链表;(5)接回原来的链表。对于第4步局部反转用到了上一篇所讲的内容。

更多算法视频讲解,你可以从以下地址找到:

  • Python编码实现:哔哩哔哩_bilibilihttps://www.bilibili.com/cheese/play/ep1509965

  • Java编码实现:哔哩哔哩_bilibilihttps://www.bilibili.com/cheese/play/ep1510007

  • Golang编码实现:数据结构笔试面试算法-Go语言版_哔哩哔哩_bilibili数据结构笔试面试算法-Go语言版,bilibili课堂,哔哩哔哩课堂,哔哩哔哩,Bilibili,B站,弹幕https://www.bilibili.com/cheese/play/ep1509945

对于链表的相关操作,我们总结了一套【可视化+图解】方法,依据此方法来解决链表相关问题,链表操作变得易于理解,写出来的代码可读性高也不容易出错。具体也可以参考视频详细讲解。

今日佳句:海内存知己,天涯若比邻。

http://www.dtcms.com/wzjs/217338.html

相关文章:

  • 天元建设集团有限公司财务报表北京优化网站推广
  • 网站开发学习什么百度指数在线查询前100
  • 新郑网站建设公司新发布的新闻
  • 做网站常用的jquery苏州seo怎么做
  • 网站的ico怎么做爱站网seo培训
  • 西安做网站哪家便宜百度搜索风云榜总榜
  • 可以做试卷并批改的网站品牌推广策略分析
  • 福建建设厅网站 资质营业推广策划方案
  • ASPJSP动态网站开发企业门户网站模板
  • 化妆品 营销型网站腾讯网qq网站
  • 网站建设情况调查表seo是哪个英文的缩写
  • 专业嵌入式软件开发杭州网站关键词排名优化
  • 网页设计外文文献优化公司排名
  • 公司网站用个人备案可以短视频剪辑培训班速成
  • 怎么写网站建设与运营电商seo引流
  • 怎么用ip做网站武汉seo群
  • 蚌埠网站建设哪家好中国广告公司前十强
  • 网站建设工作厦门seo排名扣费
  • 做设计兼职的网站百度网址大全网站
  • 网站设计用什么软件做的台州做优化
  • 做网站犯法外贸营销策略都有哪些
  • 客服网站制作湘潭网站seo
  • wordpress熊掌号专业版企业站seo外包
  • 公司网站用哪个软件做百度服务中心人工客服电话
  • 2022营业执照年审合肥网站seo费用
  • seo查询站长搜索引擎营销的简称
  • 临沂网站建设 百度优化宁波好的seo外包公司
  • 手机网站开发目的114网址大全
  • 如何注销网站备案负责人巩义网络推广
  • 东莞企业网站电话广州各区正在进一步优化以下措施