Reverse a linked list

基本題,就是 while 把指針轉向即可,然後要學會用 dummy node (好用!)

居然寫錯了.... 多練習幾次吧!

public void reverseLinkedList(ListNode head) {
    if (head == null)
        return head;

    ListNode dummy = null;
    while (head != null) {
        ListNode next = head.next;
        head.next = dummy;
        dummy = head;
        head = next;
    }
    return dummy;
}

results matching ""

    No results matching ""