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/01 Report--
This article mainly introduces the relevant knowledge of Java linked list case analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will gain something after reading this Java linked list case analysis article, let's take a look at it.
1. Delete all nodes with a value of val
Deletes all nodes in the linked list that are equal to the given value val. [OJ link]
Two pointers, prev and cur,cur, are defined to point to the next node of the header node, and prev always points to the previous node of the cur (easy to delete the node). The cur pointer is used to traverse the linked list, and the node is deleted when compared with the value value. Finally, let's compare the head nodes.
/ * Definition for singly-linked list. * public class ListNode {* int val; * ListNode next; * ListNode () {} * ListNode (int val) {this.val = val;} * ListNode (int val, ListNode next) {this.val = val; this.next = next;} * / class Solution {public ListNode removeElements (ListNode head, int val) {if (head==null) {return null;} ListNode prev=head ListNode cur=head.next; while (curving null) {if (cur.val==val) {prev.next=cur.next; cur=cur.next;} else {prev=cur; cur=cur.next;}} if (head.val==val) {head=head.next } return head;}} 2, reverse the linked list
Reverse a linked list. [OJ link]
When traversing the linked list, change the pointer of the current node to the previous node. Because a node does not reference its previous node, its previous node must be stored in advance. The latter node needs to be stored before changing the reference. Finally, return the new header reference.
/ * Definition for singly-linked list. * public class ListNode {* int val; * ListNode next; * ListNode () {} * ListNode (int val) {this.val = val;} * ListNode (int val, ListNode next) {this.val = val; this.next = next;} * / class Solution {public ListNode reverseList (ListNode head) {if (head==null) {return null;} ListNode cur=head.next Head.next=null; while (curvilinear null) {ListNode curNext=cur.next; cur.next=head; head=cur; cur=curNext;} return head;}} 3, return the middle node of the linked list
Given a non-empty single linked list with a header node, returns the middle node of the linked list. If there are two intermediate nodes, the second intermediate node is returned. [OJ link]
We can define two fast and slow pointers (fast, slow), both pointing to the header node. The fast pointer takes two steps at a time, and the slow pointer takes one step at a time. When the linked list has even nodes, slow is the intermediate node when fast= null; when the linked list has odd nodes, slow is the intermediate node when fast.next= null.
/ * Definition for singly-linked list. * public class ListNode {* int val; * ListNode next; * ListNode () {} * ListNode (int val) {this.val = val;} * ListNode (int val, ListNode next) {this.val = val; this.next = next;} * / class Solution {public ListNode middleNode (ListNode head) {if (head==null) {return null;} ListNode slow=head ListNode fast=head; while (fastening lists, null nodes) {fast=fast.next.next; slow=slow.next;} return slow;}} 4, return to the K node of the linked list
Enter a linked list and return the last but one node in the linked list. [OJ link]
This question is similar to the idea of finding intermediate nodes. Define two pointers (fast, slow). Under the premise that K is reasonable, we can let the fast pointer take KMel 1 step first, and then the fast and slow pointer goes backward at the same time. When the fast reaches the end of the linked list, the slow points to the penultimate node.
/ * public class ListNode {int val; ListNode next= null; ListNode (int val) {this.val = val;}} * / public class Solution {public ListNode FindKthToTail (ListNode head,int k) {if (k0) {if (fast.next==null) {return null;} fast=fast.next / / first let the fast node go KMui 1 step kMui;} while (fast.nextlinks null) {fast=fast.next; slow=slow.next;} return slow;}} 5, merge ordered linked lists
Merge two ordered linked lists into a single ordered linked list and return. The new linked list is formed by splicing all the nodes of the given two linked lists. [OJ link]
To solve this problem, you need to define a false node to act as the header node of the new linked list. Traverse the two nodes through the head nodes of the two linked lists, compare the values of the corresponding nodes of the two linked lists, connect the nodes with small values to the back of the new linked list, and know that the two linked lists are traversed, and when one of the linked lists is empty, connect the other linked list directly to the back of the new linked list.
Class Solution {public ListNode mergeTwoLists (ListNode list1, ListNode list2) {if (list1==null) {return list2;} if (list2==null) {return list1;} / / create a virtual node that acts as the header node of the new linked list. The value does not represent any meaning ListNode node=new ListNode (- 1); ListNode cur=node While {if (list1.val)
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.