翻转链表

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

思路:逐个扫描,翻转指针。。。个人认为翻转链表的操作是链表题目中最难最考验指针的操作。。。。

 public ListNode ReverseList(ListNode head) {
        ListNode now=head,pre=null,behind=null;
        while(now!=null){
            ListNode pNext=now.next;
        //保存尾节点,返回时就是翻转后的头结点
if(pNext==null) behind=now; now.next=pre; pre=now; now=pNext; } return behind; }


 更简单方式

 public ListNode ReverseList(ListNode head) {
        
    ListNode pre = null;
    ListNode next = null;
    while (head != null) {
        next = head.next;
        head.next = pre;
        pre = head;
        head = next;
    }
    return pre;
        
        
    }
原文地址:https://www.cnblogs.com/team42/p/6681839.html