In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what the Java chain table has, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
First question: remove linked list elements
Give you a header node head of the linked list and an integer val. Please delete all nodes in the linked list that satisfy Node.val = = val and return the new header node.
Input: head = [1, val, 2, 6, 3, 4, 5, 6]
Output: [1, 2, 3, 4, 5]
This problem is relatively simple. We just need to point the previous node of the deleted node to the last one of the deleted node. Like cur.next==cur.next.next;.
Class Solution {public ListNode removeElements (ListNode head, int val) {ListNode header=new ListNode (- 1); header.next=head; ListNode cur= header; while (cur.nextframes null) {if (cur.next.val==val) {cur.next=cur.next.next;} else {cur=cur.next;}} return header.next Question 2 reversing the linked list
Give you the header node head of the single linked list, please reverse the linked list and return to the reversed linked list.
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 4, 3, 2, 1]
This is also a simple problem, let's start with a tail node, because the next node of the last node of the linked list is a null, we can loop the next node of the latter node to the previous node.
Class Solution {public ListNode reverseList (ListNode head) {ListNode pre= null; ListNode cur=head; while (curvilinear null) {ListNode next=cur.next; cur.next=pre; pre=cur; cur=next;} return pre;}} the center node of the third linked list
Given a non-empty single linked list whose header node is head, return the middle node of the linked list.
If there are two intermediate nodes, the second intermediate node is returned.
A: this is also a simple question. We need to use the speed pointer. When the fast pointer is over, the slow node must be the midpoint. For example, the fast one can walk nine steps at a time to 18, and the slow one can walk nine steps at a time. It's just in the middle.
Class Solution {public ListNode middleNode (ListNode head) {ListNode p = head; ListNode Q = head; while (the penultimate k node of question 4) {qroomq.next.pfolp.next.} return p;}
Enter a linked list and output the last but one node in the linked list
Enter:
1, {1,2,3,4,5}
Copy
Return value:
{5}
A: this problem is also a simple one. Just work out the value of the last k point simply and rudely.
Public class Solution {public ListNode FindKthToTail (ListNode head,int k) {ListNode cur=head; ListNode pre=head; int count=0; int Xeroo; while (curving null) {cur=cur.next; count++;} if (kcount) {return null } while (predetermined null) {if (x==count-k) {break;} else {pre=pre.next; xeroids;}} return pre;}}
It's a bit troublesome to write this question, and we can also use the speed pointer to do it. One pointer takes five steps and one takes four steps.
Public class Solution {public ListNode FindKthToTail (ListNode head,int k) {ListNode paired headers; ListNode qroomheaders; for (int I = 0; I
< k; i++) { if (p != null) { p= p.next; } else { return null; } } while(p!=null){ p=p.next; q=q.next; } return q; }}第五题 合并两个有序链表 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。Input: L1 = [1Magol 2pr 4], L2 = [1BI 3pr 4]
Output: [1, 1, 2, 3, 4, 4]
A: this question deals with how to merge two linked lists. We need to merge two linked lists from big to small.
Class Solution {public ListNode mergeTwoLists (ListNode L1, ListNode L2) {ListNode dummyHead = new ListNode (0); ListNode cur = dummyHead; while (L1! = null & & L2! = null) {if (l1.val)
< l2.val) { cur.next = l1; cur = cur.next; l1 = l1.next; } else { cur.next = l2; cur = cur.next; l2 = l2.next; } } // 任一为空,直接连接另一条链表 if (l1 == null) { cur.next = l2; } else { cur.next = l1; } return dummyHead.next; }}第六题 链表分割 现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。 输入:l1 = [1,2,1,3,2] 3 输出:[1,2,1,2,3] 这道题比较难了需要我们创建两个链表,一个数大与等于x的链表,另一个数小于x的链表。然后让上一个链表的下一个尾结点指向下一个结点的尾巴结点。 这里我们需要用到如何将两个链表合并成一个链表。 做题的时候先想怎么做然后在动手! public class Partition { public ListNode partition(ListNode pHead, int x) { if(pHead == null || pHead.next == null) { return pHead; } ListNode newHead = new ListNode(0); ListNode flagHead = new ListNode(0); ListNode newh = newHead; ListNode flagh = flagHead; while(pHead != null){ if(pHead.val < x){ newh.next = new ListNode(pHead.val); newh = newh.next; }else{ flagh.next = new ListNode(pHead.val); flagh = flagh.next; } pHead = pHead.next; } newh.next = flagHead.next; return newHead.next; }}第七题 判断是否回文 对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。 给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。 1->2-> 2-> 1
Return: true
Public class PalindromeList {public boolean chkPalindrome (ListNode head) {/ / write code here ListNode fast=head; ListNode slow=head; while (fastening null & & fast.nextshipping null) {fast= fast.next.next; slow= slow.next;} ListNode cur=slow.next While (curvilinear null) {ListNode curNext=cur.next; cur.next=slow; slow=cur; cur=curNext;} / / 3. If one meets each other from back to back, it proves that palindromes while (headlines slow) {if (head. validated values. Val) {/ / first judge whether the values are equal or not return false;} if (head.next==slow) {/ / even return true;} head=head.next Slow=slow.next;} return true;} eighth intersecting linked list
Give you two single-linked list header nodes headA and headB, please find and return two single-linked list intersection of the starting node. Returns null if the two linked lists do not have intersecting nodes.
The stupid way is to calculate the number of each linked list and let the more go first.
Public class Solution {public ListNode getIntersectionNode (ListNode headA, ListNode headB) {if (headA = = null | | headB = = null) {return null;} ListNode last = headB; while (last.next! = null) {last = last.next;} last.next = headB; ListNode fast = headA; ListNode slow = headA While (fast! = null & & fast.next! = null) {slow = slow.next; fast = fast.next.next; if (slow = = fast) {slow = headA; while (slow! = fast) {slow = slow.next; fast = fast.next } last.next = null; return fast;}} last.next = null; return null }} Thank you for reading this article carefully. I hope the article "what are the Java chain questions" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.