首页 > 试题广场 >

手写代码:反转链表

[问答题]
public Node reverse(Node pHead) {
    if (pHead == null) {
        return pHead;
    }
    Node prev = null;
    while (pHead != null) {
        //保存next
        Node temp = pHead.next;
        //反转
        pHead.next = prev;
        //移动prev
        prev = pHead;
        //移动pHead
        pHead = temp;
    }
    return prev;
}
发表于 2019-08-20 15:15:32 回复(0)