Various operations of a linked list Java version __java

Source: Internet
Author: User
Tags comparable int size
Package singlelist;


Import Java.util.Stack;


/*
* YY
* 1: Single linked list inserts from scratch
* 2: Query for an element
* 3: Delete an element
* 4: Find a single linked list of the intermediate node fast method
* 5: Reverse the single linked list
* 6: The linked list inverted output
* 7: The length of the single linked list is how much
* 8: Merge two ordered single linked list head1 and head2, loop
* 9: Determine if a single linked list has a ring
* 10: Find the entry point of the ring
* 11: The length of the linked list with the ring
* 12: How to know the length of the ring
* */
Nodes of a single linked table
Class Node<t extends comparable<t>> implements comparable<node<t>>{
Private T data;
Private node<t> Next;

Public Node () {

}
Public Node (T data) {
This.data=data;
This.next=null;
}



Public T GetData () {
return data;
}
public void SetData (T data) {
This.data = data;
}
Public node<t> GetNext () {
return to Next;
}
public void Setnext (node<t> next) {
This.next = Next;
}
@Override
public int hashcode () {
final int prime = 31;
int result = 1;
result = Prime * result + ((data = null)? 0:data.hashcode ());
return result;
}


@Override
public boolean equals (Object obj) {
if (this = obj)
return true;
if (obj = null)
return false;
if (GetClass ()!= obj.getclass ())
return false;
node other = (node) obj;
if (data = = NULL) {
if (other.data!= null)
return false;
else if (!data.equals (Other.data))
return false;
return true;
}
@Override
public int compareTo (node<t> o) {
Return This.data.compareTo (O.getdata ());
}
}


/*
* This single list is not Sentinel.
* In the next release, I'll join the Sentinels.
* */


public class Singlelist<t extends Comparable<t>> {


Private node<t> head;

Public Singlelist () {
Head=null;
}

Public node<t> GetHead () {
return head;
}


Single-linked list inserts start from scratch
public void InsertData (T data) {
Node<t> node=new node<t> (data);
if (this.head==null) {
This.head=node;
}else{
Node.setnext (head);
Head=node;
}
}
Querying an Element
Public node<t> Search (T data) {
Node<t> Result=this.search (This.head, new Node (data));
return result==null?null:result;
}
Private node<t> Search (node<t> head,node<t> Element) {
Node<t> Node=head;
while (Node!=null) {
if (node.equals (element)) {
return node;
}
Node=node.getnext ();
}
return null;
}
/*
* Delete an element
* Returns TRUE if present otherwise returns false
* */
public boolean deletedata (T data) {
Return This.deletedata (This.head, new node<t> (data));
}
Private Boolean DeleteData (node<t> head,node<t> Element) {
Node<t> Node=head;
Node<t> Pre=null;
while (Node!=null) {
if (node.equals (element)) {
if (Node.equals (head)) {
This.head=node.getnext ();
}else{
Pre.setnext (Node.getnext ());
}
Node=null;
return true;
}
Pre=node;
Node=node.getnext ();
}
return false;
}
A fast method for finding intermediate nodes in a single linked list
/*
* the list is empty;


The list is not empty but has only one or two nodes; you can return the element value of the first node directly.


The list is not empty, but contains three or more nodes, you can define two pointers, one pointer skips 2 times, another pointer jumps 1 times, and when you jump to the end, the other node happens to be in the middle position.
* */
Public T Getmiddlenode () {
Node<t> Res=this.getmiddlenode (This.head);
return Res==null?null:res.getdata ();
}
Private node<t> Getmiddlenode (node<t> head) {
Node<t> Node=head;
Node<t> Halfnode=null;
if (node==null) {
return null;
}else if (Node.getnext () ==null| | Node.getnext (). GetNext () ==null) {
return node;
}else{
Halfnode=node;
while (Node.getnext ()!=null) {
Node=node.getnext ();
if (Node.getnext ()!=null) {
Node=node.getnext ();
Halfnode=halfnode.getnext ();
}
}
return halfnode;
}
}


Reversing a single list
public void Inverse () {
This.inverse (This.head);
}

private void Inverse (node<t> head) {
Node<t> Node=head.getnext ();
Node<t> Pnode=null;
This.head.setNext (NULL);
while (Node!=null) {
Pnode=node;
Node=node.getnext ();
Pnode.setnext (This.head);
This.head=pnode;
}
}

Invert the linked list to output
public void Inversetraverse () {
This.inversetraverse (This.head);
}
private void Inversetraverse (node<t> head) {
Node<t> Node=head;
Stack<node<t>> stack=new stack<node<t>> ();
while (Node!=null) {
Stack.push (node);
Node=node.getnext ();
}
while (!stack.isempty ()) {
System.out.print (Stack.pop (). GetData () + "");
}
System.out.println ();
}


Traversal of a single linked list
public void Travense () {
Travense (This.head);
}
private void Travense (node<t> head) {
Node<t> Node=head;
while (Node!=null) {
System.out.print (Node.getdata () + "");
Node=node.getnext ();
}
}

What is the length of a single linked list?
public int size () {
return size (This.head);
}
private int size (node<t> head) {
Node<t> Node=head;
int count=0;
while (Node!=null) {
count++;
Node=node.getnext ();
}
return count;
}

Combine two ordered single linked list head1 and head2, loop
Public node<t> mergesortedlist (node<t> head1,node<t> head2) {
if (head1==null) return head2;
if (head2==null) return head1;
Node<t> Target=null;
if (Head1.getdata (). CompareTo (Head2.getdata ()) <0) {
Target=head1;
Head1=head1.getnext ();
}else{
Target=head2;
Head2=head2.getnext ();
}
Target.setnext (NULL);
Node<t> mergehead=target;//record the header pointer after connection
while (Head1!=null&&head2!=null) {
if (Head1.getdata (). CompareTo (Head2.getdata ()) <0) {
Target.setnext (HEAD1);
Head1=head1.getnext ();
}else{
Target.setnext (HEAD2);
Head2=head2.getnext ();
}
Target=target.getnext ();
Target.setnext (NULL);
}
if (head1==null) {
Target.setnext (HEAD2);
}else{
Target.setnext (HEAD1);
}
return mergehead;
}

public void Mergetraverse (node<t> Node) {
This.travense (node);
}

Determine if there are rings in a single linked list: Hascycle
/*
* Set two pointers (fast, slow), the initial values are pointing to the head, slow each before the further, fast each step forward two steps,
* If there is a ring in the list, then fast must first enter the ring, and after slow into the ring, two pointers must meet.
* (of course, the fast header to the tail is null, then the ring-free list)
* */
public Boolean hascycle () {
Return this.hascycle (This.head);
}
Private Boolean hascycle (Node<t> head) {
Boolean flag=false;
Node<t> Slow=head;
Node<t> Fast=head;
while (Fast!=null&&fast.getnext ()!=null) {
Slow=slow.getnext ();
Fast=fast.getnext (). GetNext ();
if (slow.equals (fast)) {
Flag=true;
Break
}
}
return flag;
}

/*
* Find the entry point of the ring
* Assuming that the total length of the single linked list is L, the distance between the head node and the ring inlet is a, the distance between the ring inlet and the slow pointer is x, and the length of the ring is R.
* The slow pointer altogether walked the s step, then the quick pointer walked 2s step.
* In addition, the quick pointer to catch up with the slow pointer of the quick pointer to at least in the loop around a circle more (assuming that the n circle plus x distance), the following relationship:
s = a + x;
2s = a + nr + x;
=>a + x = nr;
=>a = Nr-x;
*/

Find the entry point of the ring
Private node<t> Findloopport (node<t> head)
{
Node<t> Slow=head,fast=head;
Get the meeting point
while (Fast!=null && fast.getnext ()!=null)
{
Slow=slow.getnext ();
Fast=fast.getnext (). GetNext ();
if (slow.equals (FAST))
Break
}
if (fast==null| | Fast.getnext () ==null)
return null; No ring
Slow point to the beginning, fast at the meeting point
Get the entry point.
Slow=head;
while (!slow.equals (fast)) {
Slow=slow.getnext ();
Fast=fast.getnext ();
}
return slow;
}
/*
* How to know the length of the ring
* Record the collision point of question 1, p,slow, fast from that point, the number of operations traversed is the length of the loop s.
* */
public int getcyclelength () {
Return This.getcyclelength (This.head);
}
private int getcyclelength (node<t> head) {
Node<t> P=this. Findloopport (head);
if (p==null) return 0;//no ring
Node<t> slow=p;
Node<t> fast=p;
int count=0;
while (fast!=null&& Fast.getnext ()!=null) {
Slow=slow.getnext ();
Fast=fast.getnext (). GetNext ();
count++;
if (slow.equals (fast)) {
Break
}
}
return count;
}
Length of linked list with ring
public int getcyclelistlength () {
Return Getcyclelistlength (This.head);
}
private int getcyclelistlength (node<t> head) {
Node<t> Pnode=this. Findloopport (head);
if (pnode==null) {
Return This.size (head);
}
int count=0;//The distance from the record header node to the access point
Node<t> Node=head;
while (!node.equals (Pnode)) {
count++;
Node=node.getnext ();
}
int Count1=this.getcyclelength (head);//Get the length of the ring
return count+count1;
}

Determine if two single linked lists intersect
If a simple judgment intersects, just see if the last pointer is equal
/*
* A total of two solutions
* First: Connect two linked lists to see if there is a ring in List 2. If there is a ring, the first entry point is the point of intersection
*
* */
A single linked list is not a ring
/*
* Two The nature of the Intersect of a list
* (1) Once the two lists intersect, then the nodes in the two list must have the same address.


(2) Once the two lists intersect, then two lists from the intersection node to the tail node must all be the same node.
* Solving Ideas
* First go through the first list to his tail, and then point the trailing next pointer to the second list (the tail pointer's next is pointing to null).
* So two linked lists to synthesize a linked list, to determine whether the original two linked lists intersect also changed to determine whether the new linked list has a ring problem: that is, to determine whether a single linked list has a ring
* */
Public node<t> isintersectbyconnect (node<t> head1,node<t> head2) {
Get the last node of the current list
Node<t> Target=null;
if (head1!=null) {
while (Head1.getnext ()!=null) {
Head1=head1.getnext ();
}
Head1.setnext (HEAD2);
Target=head1;
}else if (head2!=null) {
while (Head2.getnext ()!=null) {
Head2=head2.getnext ();
}
Head2.setnext (HEAD1);
Target=head2;
}
return this. Findloopport (HEAD2);
}

Public node<t> isintersect (node<t> head1,node<t> head2) {
Node<t> Target=null;
if (head1==null| | Head2==null) return target;
Boolean pos1=this.hascycle (HEAD1);
Boolean pos2=this.hascycle (HEAD2);
A linked list has a ring, another list has no ring, and there must be no intersection.
if (pos1==false&&pos2==true| | POS1==FALSE&AMP;&AMP;POS2==TRUE) return null;
int len1=this.size (HEAD1);
int len2=this.size (HEAD2);
if (len1>=len2) {
for (int i=0;i<len1-len2;i++) {
Head1=head1.getnext ();
}
}else{
for (int i=0;i<len2-len1;i++) {
Head2=head2.getnext ();
}
}
while (Head1!=null&&head2!=null) {
if (Head1.equals (head2)) {
Target=head1;
Break
}
else{
Head1=head1.getnext ();
Head2=head2.getnext ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.