Remove a node from a headless single-linked list

Source: Internet
Author: User

1. Remove nodes from the headless single-linked lista single-linked list without a head pointer. A pointer to a node in the middle of the single-linked list (not the first or last node). Remove the node. a-->b-->c-->d ===== "A-->c-->d
problem Analysis: Because only one-way traversal, so with the deletion of B-node, it is impossible to know the precursor a B, that is, the same as the normal deletion of the next point A to C;here is a trick, C as B, and B copy to C, then delete C node;

2, given a list of the head pointer, it is required to traverse only once, the order of the elements in the single linked list reversed.
a-->b-->c-->d ===== "a<--b<--c<--d
problem Analysis: Because only one-way traversal, assuming that the a-->b has been reversed to complete, now realize b<--c; by C.pnext = B, so each cycle needs to retain the values of the B and C nodes;

And after flipping, C can no longer point to D will appear broken chain list, so before the reversal should pay attention to the retention of D, that is c.pnext;

class listnode{int pValue;  ListNode Pnext;  public ListNode (int pvalue,listnode pnext) {this.pnext = Pnext; This.pvalue = PValue; }}public class Linklistex {public ListNode reverse (ListNode phead) {if (Phead = null | | phead.pnext = = NULL) retur  n Phead;  ListNode Pnodea = Phead;  ListNode pnodeb = Phead.pnext;    ListNode Pnodec = PHead.pNext.pNext;    Phead.pnext = null;      while (pnodeb! = null) {Pnodeb.pnext = Pnodea;   Pnodea = Pnodeb;      Pnodeb = Pnodec;  if (pnodeb! = null) Pnodec = Pnodeb.pnext; } return Pnodea;    } public void Delete (ListNode pnode) {if (Pnode = = NULL | | pnode.pnext = = NULL) return;    Because the difficulty is simplified in the problem set, assuming that Pnode is neither the head node nor the tail nodes, it is not allowed to judge the boundary listnode pnext = Pnode.pnext;  Pnode.pvalue = Pnext.pvalue; Pnode.pnext = Pnext.pnext;  } public static void Main (string[] args) {ListNode P5 = new ListNode (5, NULL);  ListNode P4 = new ListNode (4, p5);  ListNode p3 = new ListNode (3, p4);  ListNode P2 = new ListNode (2, p3); ListNode P1 = new ListnOde (1, p2);    Linklistex Linklistex = new Linklistex ();//Linklistex.print (Linklistex.reverse (p1));  Linklistex.delete (p3); Linklistex.print (p1);   public void print (ListNode phead) {while (phead! = null) {System.out.println (Phead.pvalue + "==>");  Phead = Phead.pnext; } }}



Remove a node from a headless single-linked list

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.