反转链表

来源:互联网 发布:承兑汇票 知乎 编辑:程序博客网 时间:2024/06/06 10:06

剑指offer No.16,前边逆序输出链表的那题就可以将链表反转后,直接输出就行,当时是用栈实现的,现在算是圆满了

package com.zjy.sword2offer;public class ReverseList {public static ListNode reverseList(ListNode head){if(head==null)return null;ListNode pre = null;ListNode current = head;while(current!=null){ListNode next = current.next;if(next==null)head = current;current.next = pre;pre = current;current = next;}return head;}public static void main(String[] args) {// TODO Auto-generated method stub}}


0 0