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

电子商务专业网站设计seo教学免费课程霸屏

电子商务专业网站设计,seo教学免费课程霸屏,wordpress 2.9下载,国外网站建设公司概述 链表的题目没有太难的算法,纯看熟练度,是必须会。面试笔试不会是直接挂的,或者给面试官留下不好的印象。 单双链表的反转,单链表实现队列,K个一组反转链表。 单链表反转 链表节点的定义 Data public class Li…

概述

链表的题目没有太难的算法,纯看熟练度,是必须会。面试笔试不会是直接挂的,或者给面试官留下不好的印象。
单双链表的反转,单链表实现队列,K个一组反转链表。

单链表反转

链表节点的定义

@Data
public class ListNode<T> {public T val;public ListNode<T> next;ListNode() {}ListNode(T val) { this.val = val; }ListNode(T val, ListNode<T> next) { this.val = val; this.next = next; }// 链表对数器// 用数组创建单链表public static <T> ListNode<T> build(T[] arr){if(arr == null || arr.length == 0){return null;}// 创建虚拟头节点ListNode<T> dummy = new ListNode<>();ListNode<T> cur = dummy;for (T item : arr) {cur.next = new ListNode<>(item);cur = cur.next;}return dummy.next;}// 重写toString方法@Overridepublic String toString(){StringBuilder sb = new StringBuilder();sb.append(this.val);ListNode<T> next = this.next;while (next != null){sb.append(" -> ").append(next.val);next = next.next;}return sb.toString();}
}

单链表反转,leetcode: https://leetcode.cn/problems/reverse-linked-list/description/

 /*** 单链表的反转* <a href="https://leetcode.cn/problems/reverse-linked-list/description/">...</a>* null-1-2-3-4-5* pre = null* head = 1* next = head.next* head.next = pre* pre = head* head = next* 先记住下一个值,然后改变指针方向。然后pre和head各流转到下一个值*/public ListNode reverseList(ListNode head){if(head == null){return head;}ListNode pre = null;ListNode next = null;while(head != null) {next = head.next;head.next = pre;pre = head;head = next;}return head;}

双链表的反转

双链表节点的定义

public class DoubleNode<V> {V val;DoubleNode<V> next;DoubleNode<V> last;DoubleNode() {}DoubleNode(V val) { this.val = val; }DoubleNode(V val, DoubleNode<V> next, DoubleNode<V> last) { this.val = val; this.next = next; this.last = last;}}

双链表反转

/*** 双链表的反转* -* 思路和单链表一样,只不过是多了一个指针。* 先记住下一个值,然后改变指针方向。然后pre和head各流转到下一个值*/public DoubleNode reverseDoubleList(DoubleNode head){if(head == null){return head;}DoubleNode pre = null;DoubleNode next = null;while(head != null) {next = head.next;head.next = pre;head.last = next;pre = head;head = next;}return head;}

单链表实现队列

class MyQueue<V>{// head记录队列的头节点private ListNode<V> head;// tail记录队列的尾节点private ListNode<V> tail;// 队列大小private int size;// 判空public boolean isEmpty(){return this.size == 0;}// 获取队列长度public int size(){return this.size;}/*** 元素压入队列* 更新head,tail,size* 思路:* 压入第一个元素,比如1,那么head=1,tail=1;* 压入第二个元素,比如2,那么1-2,即head=1, tail=2;* 压入第三个元素,比如3,那么1-2-3,即head=1, tail=3;* ...* 压入第i个元素,比如i,那么1-2-3...-i,即head=1,tail=i;* 每次压入元素时,原来的tail元素指向新压入的元素。即tail.next = cur;* tail都会更新,即tail = cur;* @param value 元素*/public void offer(V value){ListNode<V> cur = new ListNode<>(value);if(tail == null){head = cur;tail = cur;}else{tail.next = cur;tail = cur;}size++;}/*** 元素弹出队列* 遵循队列,先进先出的特点,所以每次弹出的都是队列的head* 思路:* 如果队列不为空* 比如,当前队列是:1-2-3-4-5,head=1,tail=5;* 此时弹出,那么head会更新,head = head.next;** 如果队列为空* 那么head = null; 此时注意tail要和head保持一致,否则会出现head=null,但是tail=5的情况*/public V poll(){V res = null;if(head != null){res = head.val;head = head.next;size--;}else{tail = head;}return res;}// 返回队列头部元素public V peek(){if(head != null){return head.val;}return null;}}

K个一组反转链表

力扣hard: https://leetcode.cn/problems/reverse-nodes-in-k-group/description/
step1 : 返回第k个元素

public static ListNode getKGroupEnd(ListNode start, int k){int count = 1;while(count < k && start != null){start = start.next;count++;}return start;}

step2 : 反转链表

public static void reverse(ListNode start, ListNode end){// 反转的while循环边界 cur != end.nextend = end.next;// 反转链表经典3指针ListNode cur = start;ListNode pre = null;ListNode next = null;// 先记录下一节点,指针反转。pre,cur指针流转到下一节点while (cur != end){next = cur.next;cur.next = pre;pre = cur;cur = next;}// 反转完成后再改变起始节点的next指针start.next = end;}

step3: 完整流程。拼接各组的操作。

 public static ListNode reverseKGroup(ListNode head, int k){// 先找到第一组的k个节点ListNode start = head;ListNode end = getKGroupEnd(start, k);if(end == null){// 不够k个,所以直接返回return head;}// 记录head,并且head不会再改变了head = end;reverse(start, end);ListNode lastEnd = start;// 循环找剩下的while(lastEnd.next != null){start = lastEnd.next;end = getKGroupEnd(start, k);if(end == null){// 不够k个,所以直接返回return head;}reverse(start, end);// 和上一组连接起来lastEnd.next = end;lastEnd = start;}return head;}
http://www.ds6.com.cn/news/25725.html

相关文章:

  • 宁波专业的网站搭建公司1688官网入口
  • 免费wap建站市场营销活动策划方案
  • 濮阳做网站的公司有哪些百度关键词优化的意思
  • 做网约车网站石家庄seo全网营销
  • 深圳网站设计是什么平面设计培训费用一般是多少
  • wordpress 后台样式修改杭州余杭区抖音seo质量高
  • 福安网站开发西安seo外包
  • 网站文件夹名称全网最低价24小时自助下单平台
  • 荆州做网站开源cms建站系统
  • 企业传统的网络营销推广方法南宁seo优化公司排名
  • 网页升级紧急跳转广州网站优化方式
  • 大学生家教网站开发网络销售推广公司
  • 展会搭建公司有哪些seo诊断工具有哪些
  • 宁波建设工程主管部门网站北京seo收费
  • jquery 的网站模板教育培训机构加盟十大排名
  • 网站制作技术人员搜了网推广效果怎么样
  • 网站建设 毕业论文sem和seo
  • wordpress电商主题下载娄底地seo
  • flash做ppt的模板下载网站有哪些游戏推广员上班靠谱吗
  • 网站怎么查建设银行卡号香港头条新闻
  • 私人做网站要多少钱重庆seo招聘
  • 做百度网站每年的费用多少钱营销关键词有哪些
  • 酒店网站策划书成人计算机速成培训班
  • 网站首页设计欣赏株洲网站设计
  • 手机网页自动跳转怎么处理深圳seo网站优化公司
  • 手表到哪个网站买四川seo推广
  • 做网站如何收集资料职业技能培训网上平台
  • 网站的服务器和空间seo排名的影响因素有哪些
  • 重庆刮刮卡制作西安搜索引擎优化
  • 别人做的网站自己想更新推广赚钱的app