链表翻转非递归实现

链表翻转非递归实现_第1张图片

通过dummy,current两个节点来实现非递归的实现,主要思路就是每次遍历一个节点,让current的节点首先指向之前的dummy节点,然后dummy节点再指向current节点,current节点继续向后遍历。

//非递归
    public ListNode1 ReverseListZcf(ListNode1 head) {

        if(head == null) return head;

        ListNode1 dummy = new ListNode1(0);
        dummy.next = head;
        ListNode1 current = head.next;
        head.next = null;

        while(current!=null){
            ListNode1 next = current.next;
            current.next = dummy.next;
            dummy.next = current;
            current = next;         
        }

        /*while(dummy!=null){
            System.out.println(dummy.val);
            dummy = dummy.next;
        }*/

        return dummy.next;
    }

你可能感兴趣的:(链表翻转非递归实现)