Delete the reciprocal k nodes in a single linked list and a double linked list __ single linked list

Source: Internet
Author: User
Tags static class

The complete code for implementation is as follows:

Delete the reciprocal k node in a single linked list and double linked list public class deletelist{//single linked list node definition public static class node{int value;

        Node Next;
        public Node (int data) {this.value=data;
    	}///delete the reciprocal K node in the single linked list the public static int Deletlist_k (node Head,int k) {node t=head;//point to the head node of the linked list with T if (head==null| |  k<1) {return-10000; Represents a data that has not been deleted} int leng=0;
            Calculates the length of a linked list while (t!=null) {t=t.next;
        ++leng; }//system.out.println (Leng);  Print out the length of the list is 3 if (K>leng) {return-10000;  Represents data that has not been deleted} for (int i=0;i<leng-k-1;i++) {head=head.next;  Find the penultimate k-1 node p=head.next; Find the reciprocal k-node head.next=head.next.next;
    Delete the reciprocal k node return p.value;
        }//The definition of a double linked list node is public static class node2{int value;

        Node2 Next,pre;
     public Node2 (int data) {   	This.value=data;
   	 }///delete the reciprocal K node in the double list of nodes public static int Deltetwolist_k (Node2 head2,int K) {Node2 t=head2;///T point to the head node of the linked list if (head2==null| |
   	 k<1) {return-10000; int leng=0;
        Calculates the length of the double linked list while (t!=null) {t=t.next;
      ++leng; }//system.out.println (Leng);  Print out the length of the list is 3 if (K>leng) {return-10000;
      Represents data that has not been deleted} for (int i=0;i<leng-k;i++) {head2=head2.next; } Node2 p=head2;  The reciprocal k-node p.pre.next=p.next;

   Delete the reciprocal k node return p.value;
		public static void Main (String []args) {//delete single linked list] K-node test linked list is 1->2->3 node node=new node (1);
		Node.next=new Node (2);
		Node.next.next=new Node (3);
		Node.next.next.next=new Node (4);
      
       System.out.println (Deletlist_k (node,2));
		Delete double linked list k node test list for 1->2->3 Node2 node2=new Node2 (1);
		Node2.next=new Node2 (2);
  Node2.next.next=new Node2 (3);      Node2.next.pre=node2;
		Node2.next.next.pre=node2.next;
		Node.next.next.next=new Node (4);
      
	System.out.println (Deltetwolist_k (node2,2)); }
}

Code for Zoshin:

public class Problem_02_removelastkthnode {public static class Node {public int value;

		Public Node Next;
		public Node (int data) {this.value = data; } public static node Removelastkthnode (node head, int lastkth) {if (head = = NULL | | lastkth < 1) {return he
		Ad
		Node cur = head;
			while (cur!= null) {lastkth--;
		cur = cur.next;
		} if (lastkth = 0) {head = Head.next;
			} if (Lastkth < 0) {cur = head;
			while (++lastkth!= 0) {cur = cur.next;
		} cur.next = Cur.next.next;
	return head;
		public static class Doublenode {public int value;
		Public Doublenode last;

		Public Doublenode Next;
		public doublenode (int data) {this.value = data;
			} public static Doublenode Removelastkthnode (doublenode head, int lastkth) {if (head = = NULL | | lastkth < 1) {
		return head;
		} doublenode cur = head;
			while (cur!= null) {lastkth--;
		cur = cur.next;
	} if (lastkth = 0) {head = Head.next;		Head.last = null;
			} if (Lastkth < 0) {cur = head;
			while (++lastkth!= 0) {cur = cur.next;
			} doublenode newnext = Cur.next.next;
			Cur.next = Newnext;
			if (Newnext!= null) {newnext.last = cur;
	} return head;
		public static void Printlinkedlist (Node head) {System.out.print ("linked List:");
			while (head!= null) {System.out.print (Head.value + "");
		head = Head.next;
	} System.out.println ();
		public static void Printdoublelinkedlist (Doublenode head) {System.out.print ("Double linked List:");
		Doublenode end = null;
			while (head!= null) {System.out.print (Head.value + "");
			end = head;
		head = Head.next;
		} System.out.print ("|");
			while (end!= null) {System.out.print (End.value + "");
		end = End.last;
	} System.out.println ();
		public static void Main (string[] args {node head1 = new Node (1);
		Head1.next = new Node (2);
		Head1.next.next = new Node (3); Head1.next.next.next = new Node (4);
		Head1.next.next.next.next = new Node (5);
		Head1.next.next.next.next.next = new Node (6);
		Printlinkedlist (HEAD1);
		Head1 = Removelastkthnode (Head1, 3);
		Head1 = Removelastkthnode (head1, 6);
		Head1 = Removelastkthnode (head1, 7);

		Printlinkedlist (HEAD1);
		Doublenode head2 = new Doublenode (1);
		Head2.next = new Doublenode (2);
		Head2.next.last = head2;
		Head2.next.next = new Doublenode (3);
		Head2.next.next.last = Head2.next;
		Head2.next.next.next = new Doublenode (4);
		Head2.next.next.next.last = Head2.next.next;
		Head2.next.next.next.next = new Doublenode (5);
		Head2.next.next.next.next.last = Head2.next.next.next;
		Head2.next.next.next.next.next = new Doublenode (6);
		Head2.next.next.next.next.next.last = Head2.next.next.next.next;
		Printdoublelinkedlist (HEAD2);
		Head2 = Removelastkthnode (head2, 3);
		Head2 = Removelastkthnode (head2, 6);
		Head2 = Removelastkthnode (head2, 7);

	Printdoublelinkedlist (HEAD2);
 }

}



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.