当前位置:   article > 正文

Java数据结构LinkedList单链表和双链表模拟实现及相关OJ题秒AC总结知识点_java linkedlist单循环链表

java linkedlist单循环链表

本篇文章主要讲述LinkedList链表中从初识到深入相关总结,常见OJ题秒AC,望各位大佬喜欢


一、单链表

1.1链表的概念及结构

1.2无头单向非循环链表模拟实现

1.3测试模拟代码

 1.4链表相关面试OJ题

1.4.1 删除链表中等于给定值 val 的所有节点

1.4.2 反转一个单链表

1.4.3 给你单链表的头结点 head ,请你找出并返回链表的中间结点

1.4.4 输入一个链表,输出该链表中倒数第k个结点

1.4.5 合并俩个有序链表

二、双链表

2.1双向链表模拟实现

2.2LinkedList其他常用方法介绍

2.3ArrayList和LinkedList的区别


1.1链表的概念及结构

由于顺序表ArrayList不适合从任意位置插入或者删除元素,因此引入了LinkedList链表,链表是一种物理存储结构上非连续存储结构,也称链式存储,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。

实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:

1. 单向或者双向

 2. 带头或者不带头

 

 3. 循环或者非循环

 4.无头单向非循环链表或者无头双向链表

 在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。

1.2无头单向非循环链表模拟实现

  1. public class MySingleList {
  2. static class ListNode {
  3. public int val;
  4. public ListNode next;
  5. public ListNode(int val) {
  6. this.val = val;
  7. }
  8. }
  9. public ListNode head;
  10. public void createLink() {
  11. ListNode node1 = new ListNode(12);
  12. ListNode node2 = new ListNode(13);
  13. ListNode node3 = new ListNode(14);
  14. ListNode node4 = new ListNode(16);
  15. node1.next = node2;
  16. node2.next = node3;
  17. node3.next = node4;
  18. head = node1;
  19. }
  20. /**
  21. * @author 徐延焜xyk
  22. * @Description://遍历链表
  23. */
  24. public void display() {
  25. ListNode cur = head;
  26. while (cur != null) {
  27. System.out.println(cur.val + " ");
  28. cur = cur.next;
  29. }
  30. System.out.println();
  31. }
  32. /**
  33. * @author 徐延焜xyk
  34. * @Description://查找是否包含关键字key是否在单链表当中
  35. */
  36. public boolean contains(int key) {
  37. ListNode cur = head;
  38. while (cur != null) {
  39. if (cur.val == key) {
  40. return true;
  41. }
  42. cur = cur.next;
  43. }
  44. return false;
  45. }
  46. /**
  47. * @author 徐延焜xyk
  48. * @Description://得到单链表的长度 O(N)
  49. */
  50. public int size() {
  51. int count = 0;
  52. ListNode cur = head;
  53. while (cur != null) {
  54. count++;
  55. cur = cur.next;
  56. }
  57. return count;
  58. }
  59. /**
  60. * @author 徐延焜xyk
  61. * @Description://头插法 O(1)
  62. */
  63. public void addFirst(int data) {
  64. ListNode node = new ListNode(data);
  65. node.next = head;
  66. head = node;
  67. }
  68. /**
  69. * @author 徐延焜xyk
  70. * @Description://尾插法 O(N) 找尾巴的过程
  71. */
  72. public void addLast(int data) {
  73. ListNode node = new ListNode(data);
  74. if (head == null) {
  75. head = node;
  76. return;
  77. }
  78. ListNode cur = head;
  79. while (cur.next != null) {
  80. cur = cur.next;
  81. }
  82. cur.next = node;
  83. }
  84. /**
  85. * @author 徐延焜xyk
  86. * @Description: //任意位置插入,第一个数据节点为0号下标
  87. */
  88. public void addIndex(int index, int data) throws ListIndexOutofException {
  89. checkIndex(index);
  90. if (index == 0) {
  91. addFirst(data);
  92. return;
  93. }
  94. if (index == size()) {
  95. addFirst(data);
  96. return;
  97. }
  98. ListNode cur = findIndexSubOne(index);
  99. ListNode node = new ListNode(data);
  100. node.next = cur.next;
  101. cur.next = node;
  102. }
  103. /**
  104. * @author 徐延焜xyk
  105. * @Description:找到 index-1位置的节点的地址
  106. */
  107. private ListNode findIndexSubOne(int index) {
  108. ListNode cur = head;
  109. int count = 0;
  110. while (count != index - 1) {
  111. cur = cur.next;
  112. count++;
  113. }
  114. return cur;
  115. }
  116. private void checkIndex(int index) throws ListIndexOutofException {
  117. if (index < 0 || index > size()) {
  118. throw new ListIndexOutofException("index位置不合法!");
  119. }
  120. }
  121. /**
  122. * @author 徐延焜xyk
  123. * @Description://删除第一次出现关键字为key的节点 O(N)
  124. */
  125. public void remove(int key) {
  126. if (head == null) {
  127. return;
  128. }
  129. if (head.val == key) {
  130. head = head.next;
  131. return;
  132. }
  133. ListNode cur = searchPrev(key);
  134. if (cur == null) {
  135. return;
  136. }
  137. ListNode del = cur.next;
  138. cur.next = del.next;
  139. }
  140. /**
  141. * @author 徐延焜xyk
  142. * @Description:找到关键字key的前一个节点
  143. */
  144. private ListNode searchPrev(int key) {
  145. ListNode cur = head;
  146. while (cur.next != null) {
  147. if (cur.next.val == key) {
  148. return cur;
  149. }
  150. cur = cur.next;
  151. }
  152. return null;
  153. }
  154. /**
  155. * @author 徐延焜xyk
  156. * @Description://删除所有值为key的节点
  157. */
  158. public void removeAllKey(int key) {
  159. if (head == null) {
  160. return;
  161. }
  162. ListNode prev = head;
  163. ListNode cur = head.next;
  164. while (cur != null) {
  165. if (cur.val == key) {
  166. prev.next = cur.next;
  167. cur = cur.next;
  168. } else {
  169. prev = cur;
  170. cur = cur.next;
  171. }
  172. if (head.val == key) {
  173. head = head.next;
  174. }
  175. }
  176. }
  177. /**
  178. * @author 徐延焜xyk
  179. * @Description:保证链表当中 所有的节点 都可以被回收
  180. */
  181. public void clear() {
  182. head = null;
  183. }
  184. }

1.3测试模拟代码

  1. public static void main(String[] args) {
  2. MySingleList mySingleList = new MySingleList();
  3. //LinkedList<Integer> stack = new LinkedList<Integer>();
  4. //Queue<MySingleList.ListNode> queue = new ArrayDeque<>();
  5. mySingleList.display();
  6. System.out.println("=======");
  7. System.out.println(mySingleList.contains(90));
  8. System.out.println(mySingleList.size());
  9. System.out.println("====测试插入===");
  10. mySingleList.addLast(1);
  11. mySingleList.addLast(2);
  12. mySingleList.addLast(3);
  13. mySingleList.addLast(4);
  14. try {
  15. mySingleList.addIndex(0,1);
  16. }catch (ListIndexOutofException e) {
  17. e.printStackTrace();
  18. }
  19. mySingleList.display();
  20. System.out.println("=============");
  21. mySingleList.removeAllKey(1);
  22. mySingleList.display();
  23. }

 1.4链表相关面试OJ题

1.4.1 删除链表中等于给定值 val 的所有节点

1. 删除链表中等于给定值 val 的所有节点。
203. 移除链表元素 - 力扣(LeetCode)

  1. class Solution {
  2. public ListNode removeElements(ListNode head, int val) {
  3. if (head == null){
  4. return null;
  5. }
  6. ListNode prev = head;
  7. ListNode cur = head.next;
  8. while (cur != null){
  9. if (cur.val == val){
  10. prev.next = cur.next;
  11. cur = cur.next;
  12. }else{
  13. prev = cur;
  14. cur = cur.next;
  15. }
  16. }
  17. if (head.val == val){
  18. head = head.next;
  19. }
  20. return head;
  21. }
  22. }

1.4.2 反转一个单链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

206. 反转链表 - 力扣(LeetCode)

  1. class Solution {
  2. public ListNode reverseList(ListNode head) {
  3. if(head == null){
  4. return null;
  5. }
  6. if(head.next == null){
  7. return head;
  8. }
  9. ListNode cur = head.next;
  10. head.next = null;
  11. while(cur != null){
  12. ListNode curNext = cur.next;
  13. cur.next = head;
  14. head = cur;
  15. cur = curNext;
  16. }
  17. return head;
  18. }
  19. }

1.4.3 给你单链表的头结点 head ,请你找出并返回链表的中间结点

给你单链表的头结点 head ,请你找出并返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点。

876. 链表的中间结点 - 力扣(LeetCode)

  1. class Solution {
  2. public ListNode middleNode(ListNode head) {
  3. ListNode fast = head;
  4. ListNode slow = head;
  5. while (fast != null && fast.next != null){
  6. fast = fast.next.next;
  7. slow = slow.next;
  8. }
  9. return slow;
  10. }
  11. }

1.4.4 输入一个链表,输出该链表中倒数第k个结点

链表中倒数第k个结点_牛客题霸_牛客网 (nowcoder.com)

仅仅用一个指针进行遍历注定是没有办法很优美地实现此问题解答的,所以要用两个指针,这两个指针的位置相差k-1个距离,当快指针走到最后一个节点的时候,慢指针指向的位置就是我们要的倒数第k个节点了。思想就是这么简单了,很多链表类的题目都是活用指针就可以解决的,一个指针不可以的时候就两个指针来完成。

  1. public class Solution {
  2. public ListNode FindKthToTail(ListNode head, int k) {
  3. if (k <= 0 || head == null) {
  4. return null;
  5. }
  6. ListNode fast = head;
  7. ListNode slow = head;
  8. while (k - 1 != 0) {
  9. fast = fast.next;
  10. if (fast == null) {
  11. return null;
  12. }
  13. k--;
  14. }
  15. while (fast.next != null) {
  16. fast = fast.next;
  17. slow = slow.next;
  18. }
  19. return slow;
  20. }
  21. }

1.4.5 合并俩个有序链表

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

21. 合并两个有序链表 - 力扣(LeetCode)

  1. class Solution {
  2. public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
  3. ListNode newHead = new ListNode(0);
  4. ListNode tmp = newHead;
  5. while (head1 != null && head2 != null){
  6. if (head1.val < head2.val){
  7. tmp.next = head1;
  8. head1 = head1.next;
  9. tmp = tmp.next;
  10. }else {
  11. tmp.next = head2;
  12. head2 = head2.next;
  13. tmp = tmp.next;
  14. }
  15. }
  16. if (head1 != null){
  17. tmp.next = head1;
  18. }
  19. if (head2 != null){
  20. tmp.next = head2;
  21. }
  22. return newHead.next;
  23. }
  24. }

上述这些oj题都是最基本的题目,请关注后续播客会有难度题上线!!

二、双链表

2.1双向链表模拟实现

  1. public class MyLinkedList {
  2. static class ListNode {
  3. public int val;
  4. public ListNode prev;//前驱
  5. public ListNode next;//后继
  6. public ListNode(int val) {
  7. this.val = val;
  8. }
  9. }
  10. public ListNode head;
  11. public ListNode last;
  12. //头插法O(1)
  13. public void addFirst(int data) {
  14. ListNode node = new ListNode(data);
  15. if (head == null) {
  16. head = node;
  17. last = node;
  18. } else {
  19. node.next = head;
  20. head.prev = node;
  21. head = node;
  22. }
  23. }
  24. //尾插法O(1)
  25. public void addLast(int data) {
  26. ListNode node = new ListNode(data);
  27. if (head == null) {
  28. head = node;
  29. last = node;
  30. } else {
  31. last.next = node;
  32. node.prev = last;
  33. last = node;
  34. }
  35. }
  36. //任意位置插入,第一个数据节点为0号下标
  37. public void addIndex(int index, int data) {
  38. if (index < 0 || index > size()) {
  39. throw new ListIndexOutOfException();
  40. }
  41. if (index == 0) {
  42. addFirst(data);
  43. return;
  44. }
  45. if (index == size()) {
  46. addLast(data);
  47. return;
  48. }
  49. ListNode cur = findIndex(index);
  50. ListNode node = new ListNode(data);
  51. node.next = cur;
  52. cur.prev.next = node;
  53. node.prev = cur.prev;
  54. cur.prev = node;
  55. }
  56. public ListNode findIndex(int index) {
  57. ListNode cur = head;
  58. while (index != 0) {
  59. cur = cur.next;
  60. index--;
  61. }
  62. return cur;
  63. }
  64. //查找是否包含关键字key是否在单链表当中
  65. public boolean contains(int key) {
  66. ListNode cur = head;
  67. while (cur != null) {
  68. if (cur.val == key) {
  69. return true;
  70. }
  71. cur = cur.next;
  72. }
  73. return false;
  74. }
  75. //删除第一次出现关键字为key的节点
  76. public void remove(int key) {
  77. ListNode cur = head;
  78. while (cur != null) {
  79. if (cur.val == key) {
  80. //1. 删除的是头节点
  81. if (cur == head) {
  82. head = head.next;
  83. //只有一个节点
  84. if (head != null) {
  85. head.prev = null;
  86. }
  87. } else {
  88. //中间 尾巴
  89. cur.prev.next = cur.next;
  90. //不是尾巴节点
  91. if (cur.next != null) {
  92. cur.next.prev = cur.prev;
  93. } else {
  94. //是尾巴节点
  95. last = last.prev;
  96. }
  97. }
  98. return;
  99. }
  100. cur = cur.next;
  101. }
  102. }
  103. //删除所有值为key的节点
  104. public void removeAllKey(int key) {
  105. ListNode cur = head;
  106. while (cur != null) {
  107. if (cur.val == key) {
  108. //1. 删除的是头节点
  109. if (cur == head) {
  110. head = head.next;
  111. //只有一个节点
  112. if (head != null) {
  113. head.prev = null;
  114. }
  115. } else {
  116. //中间 尾巴
  117. cur.prev.next = cur.next;
  118. //不是尾巴节点
  119. if (cur.next != null) {
  120. cur.next.prev = cur.prev;
  121. } else {
  122. //是尾巴节点
  123. last = last.prev;
  124. }
  125. }
  126. }
  127. cur = cur.next;
  128. }
  129. }
  130. public int size() {
  131. int len = 0;
  132. ListNode cur = head;
  133. while (cur != null) {
  134. len++;
  135. cur = cur.next;
  136. }
  137. return len;
  138. }
  139. public void display() {
  140. ListNode cur = head;
  141. while (cur != null) {
  142. System.out.println(cur.val + " ");
  143. cur = cur.next;
  144. }
  145. System.out.println();
  146. }
  147. public void clear() {
  148. ListNode cur = head;
  149. while (cur != head) {
  150. ListNode curNext = cur.next;
  151. cur.prev = null;
  152. cur.next = null;
  153. cur = curNext;
  154. }
  155. head = null;
  156. last = null;
  157. }
测试代码:
  1. public static void main(String[] args) {
  2. MyLinkedList linkedList = new MyLinkedList();
  3. linkedList.addLast(1);
  4. linkedList.display();
  5. }

1. LinkedList实现了List接口
2. LinkedList的底层使用了双向链表
3. LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问
4. LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1)
5. LinkedList比较适合任意位置插入的场景

2.2LinkedList其他常用方法介绍

方法解释
boolean add(E e)尾插 e
void add(int index, E element)将 e 插入到 index 位置
boolean addAll(Collection<? extends E> c)尾插 c 中的元素
E remove(int index)删除 index 位置元素
boolean remove(Object o)删除遇到的第一个 o
E get(int index)获取下标 index 位置元素
E set(int index, E element)将下标 index 位置元素设置为 element
void clear()清空
boolean contains(Object o)判断 o 是否在线性表中
int indexOf(Object o)返回第一个 o 所在下标
int lastIndexOf(Object o)返回最后一个 o 的下标
List<E> subList(int fromIndex, int toIndex)截取部分 list
  1. LinkedList<Integer> list = new LinkedList<>();
  2. list.add(1); // add(elem): 表示尾插
  3. list.add(2);
  4. list.add(3);
  5. list.add(4);
  6. list.add(5);
  7. list.add(6);
  8. list.add(7);
  9. System.out.println(list.size());
  10. System.out.println(list);
  11. // 在起始位置插入0
  12. list.add(0, 0); // add(index, elem): 在index位置插入元素elem
  13. System.out.println(list);
  14. list.remove(); // remove(): 删除第一个元素,内部调用的是removeFirst()
  15. list.removeFirst(); // removeFirst(): 删除第一个元素
  16. list.removeLast(); // removeLast(): 删除最后元素
  17. list.remove(1); // remove(index): 删除index位置的元素
  18. System.out.println(list);

2.3ArrayList和LinkedList的区别

不同点ArrayListLinkedList
存储空间上物理上一定连续逻辑上连续,但物理上不一定连续
随机访问支持O(1)不支持:O(N)
头插需要搬移元素,效率低O(N)只需修改引用的指向,时间复杂度为O(1)
插入空间不够时需要扩容没有容量的概念
应用场景元素高效存储+频繁访问任意位置插入和删除频繁

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/707992
推荐阅读
相关标签
  

闽ICP备14008679号