Reverse single linked list __ Reverse single linked list

Source: Internet
Author: User
one, reverse single linked list
Reverse single Chain table class ListNode {int val;

    ListNode next = null;
    ListNode (int val) {this.val = val;
       } public class Solution {public ListNode reverselist (ListNode head) {ListNode rehead=null;
           while (head!=null) {ListNode tmp=head;
           Head=head.next;
           Tmp.next=rehead;
           rehead=tmp;

       System.out.println (Rehead.val);
    return rehead; }//Print single list public void Printlist (ListNode head) {while (head!=null) {System.out.print
             (head.val+ "");
        Head=head.next;
    } System.out.println ();
        public static void Main (String[]args) {//system.out.println ("Hello");
         ListNode head=new ListNode (1);
         Head.next=new ListNode (2);
         Head.next.next=new ListNode (3);
         Solution s=new Solution ();
         S.printlist (head);

    S.printlist (head) (s.reverselist); }
}

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.