链表常见的题型(java实现)

前端之家收集整理的这篇文章主要介绍了链表常见的题型(java实现)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

链表是面试中最常见的一种题型,因为他的每个题的代码短,短短的几行代码就可以体现出应聘者的编码能力,所以它也就成为了面试的重点。

链表常见的操作有1.打印链表的公共部分,2.删除链表的倒数第K个节点,3.翻转单向链表,4.环形约瑟夫环问题,5.判断链表是否是一个回文链表,6.两个链表生成相加链表,7.删除无序链表中重复出现的节点,8.删除指定值得节点,9.合并两个有序的单链表,10.环形链表的插入

  1. import java.util.*;
  2. /**********
  3. *@Author:Tom-shushu
  4. *@Description:链表问题
  5. *@Date:21:58 2019/10/2
  6. * .--,.--,* ( ( \.---./ ) )
  7. * '.__/o o\__.'
  8. * {= ^ =}
  9. * > - <
  10. * / \
  11. * // \\
  12. * //| . |\\
  13. * "'\ /'"_.-~^`'-.
  14. * \ _ /--' `
  15. * ___)( )(___
  16. * (((__) (__))) 高山仰止,景行行止.虽不能至,心向往之。
  17. *
  18. **********/
  19. public class Node {
  20. int value;
  21. public Node head;
  22. Node next;
  23. public Node( data) {
  24. this.value = data;
  25. }
  26. //打印链表的公共部分
  27. void print(Node head1,Node head2) {
  28. while (head1 != null && head2 != null) {
  29. if (head1.value < head2.value) {
  30. head1 = head1.next;
  31. } else if (head1.value > head2.value) {
  32. head2 = head2.next;
  33. } else {
  34. System.out.println(head1.value);
  35. head1 = head1.next;
  36. head2 = head2.next;
  37. }
  38. }
  39. }
  40. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  41. 删除单链表的倒数第K个节点
  42. 版本一
  43. public Node remove1(Node head, k) {
  44. if (head == null || k < 1return head;
  45. }
  46. Node cur = head;
  47. while (cur != ) {
  48. k--;
  49. cur = cur.next;
  50. }
  51. if (k == 0) {删除的是第一个
  52. head = head.next;
  53. }
  54. if (k < 0) {
  55. cur = head;
  56. while (++k != 0) {
  57. cur = cur.next;
  58. }
  59. cur.next = cur.next.next;
  60. }
  61. head;
  62. }
  63. 版本二
  64. public Node remove2(Node head,1)">null || k <= 0return ;
  65. }
  66. Node slow = head;
  67. Node fast =fast 指向 k + 1
  68. for (int i = 1; i < k + 1; i++if (fast.next != ) {
  69. fast = fast.next;
  70. } {
  71. ;
  72. }
  73. }
  74. fast指向尾部,slow指向倒数K+1,即 k 的前一个数。
  75. while (fast.next != ) {
  76. fast = fast.next;
  77. slow = slow.next;
  78. }
  79. 删除第 k 个数。
  80. slow = slow.next.next;
  81. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  82. 翻转单向链表
  83. Node reList(Node head) {
  84. Node pre = ;
  85. Node next = ;
  86. while (head != ) {
  87. next = head.next;
  88. head.next = pre;
  89. pre = head;
  90. head = next;
  91. }
  92. pre;
  93. }
  94. Node reList2(Node head) {
  95. null || head.next == head;
  96. }
  97. Node pre = head;
  98. Node newHead = while (pre != ) {
  99. Node temp = pre.next;
  100. pre.next = newHead;
  101. newHead = temp;
  102. }
  103. newHead;
  104. }
  105. 环形约瑟夫问题
  106. public Node yuesefu(Node head,1)"> m) {
  107. null || head.next == head || m < 1 head;
  108. }
  109. Node last =while (last.next != head) {
  110. last = last.next;
  111. }
  112. int count = 0while (head != last) {
  113. if (++count == m) {
  114. last.next = head.next;
  115. count = 0;
  116. } {
  117. last = last.next;
  118. }
  119. head =判断一个链表是否是回文链表
  120. boolean isHuiWen(Node head) {
  121. Stack<Node> stack = new Stack<Node>();
  122. Node cur =) {
  123. stack.push(cur);
  124. cur =if (head.value != stack.pop().value) {
  125. false;
  126. }
  127. head =true;
  128. }
  129. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  130. 两个单链表生成相加链表
  131. Node xinagjialainbiao(Node head1,Node head2) {
  132. Stack<Integer> stack1 = new Stack<Integer>();
  133. Stack<Integer> stack2 = ();
  134. ) {
  135. stack1.push(head1.value);
  136. head1 = head1.next;
  137. }
  138. while (head2 != ) {
  139. stack2.push(head1.value);
  140. head2 = head2.next;
  141. }
  142. int ca = 0int n1 = 0int n2 = 0int n = 0;
  143. Node node = ;
  144. Node pre = while (!stack1.isEmpty() || !stack2.isEmpty()) {
  145. if (stack1.isEmpty()) {
  146. n1 = 0 {
  147. n1 = stack1.pop();
  148. }
  149. (stack2.isEmpty()) {
  150. n2 = 0 {
  151. n2 = stack2.pop();
  152. }
  153. pre = node;
  154. node = new Node(n % 10);
  155. node.next = pre;
  156. }
  157. if (ca == 1) {
  158. pre =new Node(1 node;
  159. }
  160. 删除无需单链表中重复出现的节点
  161. deletecf(Node head) {
  162. ;
  163. }
  164. HashSet<Integer> set = new HashSet<Integer>();
  165. Node pre = head;
  166. Node cur = head.next;
  167. set.add(head.value);
  168. (set.contains(cur.value)) {
  169. pre.next = cur.next;
  170. } {
  171. set.add(cur.value);
  172. pre = cur;
  173. }
  174. cur = cur.next;
  175. }
  176. }
  177. 在单链表中删除指定值得节点
  178. public Node deletevalue(Node head,1)"> num) {
  179. Stack<Node> stack = num) {
  180. stack.push(head);
  181. }
  182. head =while (!stack.isEmpty()) {
  183. stack.peek().next = stack.pop();
  184. }
  185. 合并两个有序单链表(递归)
  186. Node Merge(Node list1,Node list2) {
  187. if (list1 == list2;
  188. }
  189. if (list2 == list1;
  190. }
  191. if (list1.value <= list2.value) {
  192. list1.next = Merge(list1.next,list2);
  193. list1;
  194. } {
  195. list2.next = Merge(list1,list2.next);
  196. list2;
  197. }
  198. }
  199. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==
  200. 环形链表的插入
  201. public Node insertNum(Node head,1)"> num){
  202. Node node = new Node(num);
  203. if(head == ){
  204. node.next = node;
  205. node;
  206. }
  207. Node pre = head.next;
  208. while (cur != head){
  209. if (pre.value <= num && cur.value >= num){
  210. break;
  211. }
  212. pre = cur;
  213. cur = cur.next;
  214. }
  215. pre.next = node;
  216. node.next = cur;
  217. return head.value < num ? head : node;
  218. }
  219. }

 

猜你在找的算法相关文章