Reverse Linked List 反转链表

项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

1.题目

反转一个单链表。
示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

2.解法

1.将当前节点cur的下一个节点cur.next取出来保存为tmp。
2.更改当前节点cur的下一个节点为pre,也即反转当前结点的next指针指向前。
3.将pre指针前移,更改为cur,这样下一个节点的next将指向cur。
4.将当前指针cur指向tmp。

public class ReverseLinkedList {

    public static ListNode reverse(ListNode head) {
        ListNode pre = null, cur = head;
        while (cur != null) {
            ListNode tmp = cur.next;
            cur.next = pre; // 当前结点指向前一个节点
            pre = cur; // pre指针前移
            cur = tmp; // 将cur指针指向tmp,即下一个
        }
        head.next = null;
        return pre;
    }

    public static void main(String[] args) {
        ListNode head = ListNode.initLinkList();
        ListNode newHead = reverse(head);
        ListNode.printListNode(newHead);
    }
}

public class ListNode<T> {

    public T data;
    public ListNode<T> next;

    public ListNode(T data) {
        this.data = data;
        this.next = null;
    }

    public static void printListNode(ListNode head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
    }

    public static ListNode initLinkList() {
        ListNode head = new ListNode(1);
        ListNode current = head;
        for(int i=2; i<=10; i++) {
            ListNode tmp = new ListNode(i);
            current.next = tmp;
            current = current.next;
        }
        return head;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值