2.4反转链表和双向链表

版权声明:本文采用 知识共享署名4.0 国际许可协议进行许可。 https://blog.csdn.net/youmian6868/article/details/88527678
题目

实现反转链表和双向链表

代码实现
//反转单向链表
public Node reverseList(Node head) {
    Node pre = null;
    Node next = null;

    while (head != null) {
        next = head.next;
        head.next = pre;
        pre = head;
        head = next;
    }

    return pre;
}

//反转双向链表
public DoubleNode reverseList(DoubleNode head) {
    DoubleNode pre = null;
    DoubleNode next = null;

    while (head != null) {
        next = head.next;
        head.next = pre;
        head.last = next;
        pre = head;
        head = next;
    }

    return pre;
}

猜你喜欢

转载自blog.csdn.net/youmian6868/article/details/88527678
今日推荐