反转链表

来源:互联网 发布:网络暴力有哪方面影响 编辑:程序博客网 时间:2024/06/05 06:25

输入一个链表,反转链表后,输出链表的所有元素。

运行时间:38ms 占用内存:528k

leetcode原题


/*public class ListNode {    int val;    ListNode next = null;    ListNode(int val) {        this.val = val;    }}*/public class Solution {    public ListNode ReverseList(ListNode head) {<span style="white-space:pre"></span>ListNode newhead=null;        ListNode pre=new ListNode(0);        while(head!=null){            pre=head.next;            head.next=newhead;            newhead=head;            head=pre;        }        /*while(newhead!=null){            System.out.print(newhead.val);            newhead=newhead.next;        }*/        return newhead;            }}


0 0
原创粉丝点击