6. Java implements reverse order of a single-chain table

Source: Internet
Author: User
 

Method 1

 
Public class node {int data; node next = NULL; Public node (INT data) {This. data = data;} public static node reversesinglelink (node head) {node front = NULL; node succ = NULL; node P = head. next; // do you know if it is P = head if you do not take the lead node? I still cannot figure out the header node. While (P! = NULL) {succ = P. next; p. next = front; front = P; P = succ;} return front;} public static void main (string [] ARGs) {node head = new node (0 ); node tail = head; For (INT I = 1; I <4; ++ I) {node P = new node (I); tail. next = P; tail = P;} head = reversesinglelink (head); While (Head! = NULL) {system. Out. println (head. data); Head = head. Next ;}}}

Method 3: I have not understood it yet. Recursive Method.

Public class list {node head; node tail; Public void reverse0 (list) {node last = reverse (list. head); list. tail = last; List. tail. next = NULL;} public node reverse (node HD) {If (Hd = NULL) {return NULL;} else if (HD. next = NULL) {head = HD; // The Last One becomes the head} else {node = reverse (HD. next); node. next = HD ;}return HD ;}} class node {int data; node next; Public node (int I) {DATA = I ;}}

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.