大庆建设网站首页优化大师官方下载
news/
2025/9/30 15:52:23/
文章来源:
大庆建设网站首页,优化大师官方下载,wordpress获取广告js,网站建设颜色代码表1.链表的概念 对于顺序存储的结构最大的缺点就是插入和排序的时候需要移动大量的元素#xff0c;所以链表的出生由此而来
先上代码#xff1a;
// 链表
public class LinkedListT extends Comparable {// 结点类class Node {T ele; // 当前结点上的元素内容Node ne…1.链表的概念 对于顺序存储的结构最大的缺点就是插入和排序的时候需要移动大量的元素所以链表的出生由此而来
先上代码
// 链表
public class LinkedListT extends Comparable {// 结点类class Node {T ele; // 当前结点上的元素内容Node next; // 指向下个结点的索引public Node(T ele) {this.ele ele;}Overridepublic String toString() {return ele.toString();}}// 头结点private Node head;// 链表中实际存放结点的个数private int size;// 判断链表是否为空public boolean isEmpty() {return head null;}// 获取链表中结点的个数public int getSize() {return this.size;}public void addHead(T ele) {this.add(0, ele);}public void addTail(T ele) {this.add(this.size, ele);}// 在指定位置添加结点, 关键点: 找到待插入位置的前一个结点public void add(int index, T ele) {if (index 0 || index this.size) {throw new IllegalArgumentException(index is invalid!);}Node node new Node(ele);// 给链表增加一个虚拟头结点, 解决在链表的头部添加时的特殊处理Node dummyHead new Node(null);dummyHead.next head;Node prev dummyHead;for (int i 0; i index; i) {prev prev.next;}node.next prev.next;prev.next node;// 更新头结点head dummyHead.next;this.size;}//根据索引找对应的元素public T get(int index){if(index0||indexthis.size){return null;}Node curhead;for (int i 0; i index; i) {curcur.next;}return cur.ele;}
//获取头节点的元素public T getFirst(){return get(0);}//获取尾结点的元素public T getLast(){return get(this.size-1);}//判断是否包含该元素public boolean contain(T ele){Node curhead;for (int i 0; i this.size; i) {if(cur.ele.compareTo(ele)0){return true;}curcur.next;}return false;}//将链表中的某个元素进行替换public void set(T ele,int index) {if(index0||indexthis.size){}Node curhead;for (int i 0; i index; i) {curcur.next;}cur.eleele;}//将链表中的元素进行删除public void delete(int index) {if (index 0 || index this.size) {}//无虚拟头结点
// if (index 0) {
// Node delnode head;
// head delnode.next;
// delnode.next null;
// this.size--;
// } else {
// Node pre head;
// Node cur pre.next;
// for (int i 1; i index; i) {
// pre pre.next;
// curcur.next;
// }
// Node delnode cur;
// pre.next delnode.next;
// delnode.next null;
// this.size--;
// cur cur.next;//有虚拟头节点Node dummyNodenew Node(null);dummyNode.nexthead;Node predummyNode;Node curpre.next;for (int i 0; i index; i) {prepre.next;curcur.next;}Node delnode cur;pre.next delnode.next;delnode.next null;this.size--;cur cur.next;}Overridepublic String toString() {//IO流StringBuilder sb new StringBuilder();// 头结点不能动Node cur head;while (cur ! null) {sb.append(cur --);cur cur.next;}return sb.toString();}
}
1.链表的定义 链表是由一个个结点组成的每个结点之间通过链接关系串联起来的每个结点都有一个前驱、一个后继最后一个结点的后继结点为空节点 由链接关系A-B组织起来的两个结点B称为A的后继结点A被称为B的前驱结点链表分为单向链表、双向链表、循环链表等 2.链表结点的定义 由于该类继承了Comparable,所以该类中的元素具有了比较性ele代表的是数据域可以是任意的类型由编码的人自行指定next代表指针域指向后继结点的地址 3.虚拟头结点 为了方便对链表的头结点执行操作往往会建立一个虚拟头结点这个结点上也不存储数据也就是ele字段永远为空或者是为一个特殊的标识 ListNode dummyNodenew ListNode(head,-1);//后继结点为头结点默认值为-1
2.链表的遍历
1.遍历的含义
遍历就是从链表头结点开始对所有的结点一次访问的过程
2.动画演示 虚拟头节点head用-1进行标识tmp代表当前遍历到的结点其中tmp后的数字代表链表结点的索引 3.链表结点的索引
1.索引的含义
链表结点的索引就是给定一个链表头和一个下标index,通过下标获取到链表第index个元素
2.动画演示 3.代码演示 //根据索引找对应的元素public T get(int index){if(index0||indexthis.size){return null;}Node curhead;for (int i 0; i index; i) {curcur.next;}return cur.ele;}
4.链表结点的插入
1.插入的含义 给定一个链表头和一个位置index(index0)和一个值ele生成一个值为ele的结点并且将它插入到链表第index之后的位置
2.动画演示 3.演示代码 // 在指定位置添加结点, 关键点: 找到待插入位置的前一个结点public void add(int index, T ele) {if (index 0 || index this.size) {throw new IllegalArgumentException(index is invalid!);}Node node new Node(ele);// 给链表增加一个虚拟头结点, 解决在链表的头部添加时的特殊处理Node dummyHead new Node(null);dummyHead.next head;Node prev dummyHead;for (int i 0; i index; i) {prev prev.next;}node.next prev.next;prev.next node;// 更新头结点head dummyHead.next;this.size;}
5.链表结点的删除
1.删除的含义 给定一个链表头和一个位置index(index1)将位置index的结点删除并且返回被删除的结点由于第一个结点是虚拟头结点所以我们要从索引为1的位置开始
2.动画演示 3.代码演示 //将链表中的元素进行删除public void delete(int index) {if (index 0 || index this.size) {}//无虚拟头结点
// if (index 0) {
// Node delnode head;
// head delnode.next;
// delnode.next null;
// this.size--;
// } else {
// Node pre head;
// Node cur pre.next;
// for (int i 1; i index; i) {
// pre pre.next;
// curcur.next;
// }
// Node delnode cur;
// pre.next delnode.next;
// delnode.next null;
// this.size--;
// cur cur.next;//有虚拟头节点Node dummyNodenew Node(null);dummyNode.nexthead;Node predummyNode;Node curpre.next;for (int i 0; i index; i) {prepre.next;curcur.next;}Node delnode cur;pre.next delnode.next;delnode.next null;this.size--;cur cur.next;}6.链表结点的查找
1.查找的含义
查找的含义就是给定一个值通过遍历链表找到链表值和给定相等的那个结点
2.动画演示 3.代码演示 //根据索引找对应的元素public T get(int index){if(index0||indexthis.size){return null;}Node curhead;for (int i 0; i index; i) {curcur.next;}return cur.ele;}
7.链表结点的修改
1.修改的含义 给定一个链表头一个位置index(index1)和一个值ele将位置index的结点值修改为ele
2.代码演示 //将链表中的某个元素进行替换public void set(T ele,int index) {if(index0||indexthis.size){}Node curhead;for (int i 0; i index; i) {curcur.next;}cur.eleele;}
leetcode题单
返回倒数第k个节点 // 双指针public int kthToLast(ListNode head, int k) {ListNode fast head;ListNode slow head;// 让快指针先走k步for (int i 0 ; i k ; i) {fast fast.next;}// 再让快指针和慢指针同时移动// 当快指针走到链表结尾时// 慢指针所指向的节点就是目标节点while (fast ! null) {fast fast.next;slow slow.next;}return slow.val;}
删除链表的倒数第N个节点 //利用双指针public ListNode removeNthFromEnd(ListNode head, int n) {if(headnull||n0){return head;}ListNode dummyNodenew ListNode(0,head);ListNode slowdummyNode;ListNode fastdummyNode;for (int i 0; i n1; i) {fastfast.next;}while(fast!null){slowslow.next;fastfast.next;}slow.nextslow.next.next;return dummyNode.next;}
反转链表 public ListNode reverseList(ListNode head) {if(headnull||head.nextnull){return head;}ListNode resNodereverseList(head.next);head.next.nexthead;head.nextnull;return resNode;}public ListNode reverseList(ListNode head) {//如果头结点为空或者是头结点的后继节点为空的话直接返回头结点if(headnull ||head.nextnull){return head;}//定义指针ListNode curhead;ListNode tempcur.next;ListNode prenull;while(cur!null){tempcur.next;cur.nextpre;precur;curtemp;}return pre;}
删除链表中的节点 public void deleteNode(ListNode node) {node.valnode.next.val;node.nextnode.next.next;}
两两交换链表中的节点 //方法一public ListNode swapPairs(ListNode head) {if(headnull||head.nextnull){return head;}ListNode lefthead;ListNode rightleft.next;ListNode newNodeswapPairs(right.next);left.nextnewNode;right.nextleft;return right;}//方法二public ListNode swapPairs(ListNode head) {if(headnull||head.nextnull){return head;}ListNode rhead;for (int i 0; i 2; i) {if(rnull){return head;}rr.next;}ListNode nodereverse(head,r);head.nextswapPairs(r);return node;}public ListNode reverse(ListNode head,ListNode right){ListNode prenull,curNodehead,nextnull;while(curNode!right){nextcurNode.next;curNode.nextpre;precurNode;curNodenext;}return pre;
}//方法三public ListNode swapPairs(ListNode head) {//对入参进行判断if(headnull||head.nextnull){return head;}ListNode dummyHeadnew ListNode(Integer.MIN_VALUE);dummyHead.nexthead;ListNode predummyHead;ListNode curpre.next;ListNode nextcur.next;while(cur!nullnext!null){cur.nextnext.next;next.nextcur;pre.nextnext;precur;curpre.next;nextcurnull?null:cur.next;}return dummyHead.next ;}//方法四public ListNode swapPairs(ListNode head) {if(headnull||head.nextnull){return head;}//将头结点的后继点设置为resListNode res head.next;//将res.next和res.next.next进行交换head.next swapPairs(res.next);//原先的res与头结点相连接res.next head;//返回对应的resreturn res; }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/923027.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!