Wei head Print linked list

Source: Internet
Author: User

Title Description Enter a list of values for each node of the list to print from the end of the header.
Input Description:
Enter the table header for the linked list


Output Description:
Output as a "new linked list" header that needs to be printed


/** *    public class ListNode { *        int val; *        ListNode next = null; * *        ListNode(int val) { *            this.val = val; *        } *    } * */ import java.util.ArrayList; import java.util.Stack; public class Solution {      public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {              ArrayList<Integer>  result= new ArrayList();          if (listNode== null )              return result;          Stack<ListNode> stack= new Stack();          stack.push(listNode);          while (listNode.next!= null )          {              stack.push(listNode.next);              listNode=listNode.next;          }          while (!stack.isEmpty())          {              ListNode node=stack.pop();              result.add(node.val);          }          return result;      } }Single linked list of linked list structure Linked list:A data storage structure. Learn the list first to understand the array, according to friends, the relationship between the array and the linked list is equivalent to QQ2008 and QQ2009.
You can use a linked list instead of an array in most cases, unless you want to access the data frequently by index.
The list of links mainly involves: single-linked list, double-ended linked list, ordered linked list, doubly linked list, and an iterator-linked list (an iterator is a method used to randomly access the list element).
Because of the previous playful data structure did not attend class, now regret so try to fill in.

link node:In a linked list, each data item is included in the link. A link is an object of a class that can be called link. And each Link object contains a field (usually called next) that references the next link node. But the list (linklist) There is a field (first) in the object itself that points to a reference to the initial link node. It will be clearer to see the picture.
This table is represented in the code with two construction classes:
Link (Link node class)ClassLink {
PublicIntIData;
PublicDoubleDData;
Public Link Next; // This next is the link node object's reference to the next link. Default initialized to NULL
public Link (int ID, double dd) {
IData = ID;
DData = dd;
}
}Linklist (linked list Class) class linklist {
public Link first; // node first, initialized to null
public linklist () {
First = null;
}
}The reference to the next link node in such a link makes up the entire list.
Today, this example shows a single linked list. The main operations are as follows:
inserts a data item in the linked list header.
Deletes a data item in the linked list header.
Traverse the list to display the contents.
First insert a link contact
The logic is to point the reference of first to the next reference of the link object chain node, and then point first to the link node to construct a new list.

The Code large box is as follows:PublicvoidInsertfirst (IntIdDoubleDD) {
Link NewLink= new link ( ID,&NBSP;DD);  //  constructs a new link node object
    newlink.next =< Span style= "color: #000000;" > first; //  the Link node object's next point to the first reference Span style= "color: #008000;" >
    first = newlink;      //   then points first to the NewLink object
/span> The logic for deleting a link node is to store the first reference (the link node to be deleted) with a temporary variable and then point first to First.next (that is, the link to which the next node of the link object He only wants to point). The node that is to be deleted has no reference to the pointer. Java garbage collection will take him back. Implements and returns the stored Delete node.
The Code large box is as follows:PublicLink Deletefirst () {
Link Temp= First ; // temporarily Save this link node (that is, to be deleted) from the first reference
First = first.next; Point first to the next reference of the link link that he refers to
return temp; // returns the link node to be deleted
}

This makes it clear that the relationship between Java object references is easy to understand. The following is the entire code for the implementation:

Code

The results are printed as follows:

List:(First---> Last) {88,88.88}{66,66.88}{44,44.88}{22,22.88}
{88,88.88}  deleted
{ 66,66. 88}  deleted
{ 44,44. 88}  deleted
{ 22,22. 88}  deleted
list:  (first ---> last)

Continue to expand the Find and Delete methods under Add Lookup key values and delete link nodes by corresponding key values.
Find Method:
This method is similar to the DisplayLink method above. Defines current as first, compares it by constant current.next.iData with the key value, and returns the present reference if it is equal.
Returns null if it is not found until the last null
Delete method:
This method requires two variables. Current: Reference privious: A reference to the previous link node. This method is also searched by loop if found
A reference to the current next from the previous reference is available. See figure

Final code:

Code

Execution Result:

List:(First---> Last) {88,88.88}{66,66.88}{44,44.88}{22,22.88}
The FindItem:{22,22.88} The DelItem:{22,. *
List: (First---> Last) {. *}{ina. *}{toa. *

Wei head Print 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.