算法練習_LeetCode_鏈表1

最近有空就在LeetCode上刷下題,在工作中雖然很少自已寫算法,JDK已經的封裝好了,直接拿來就用,但是平常有空刷下題對於理解這些封裝的集合API及選用還是挺有幫助的。畢竟不是計算機科班出身的,數據結構與算法平常還是要多補下。


  • 刪除鏈表倒數第n個數

Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        //計算長度
        ListNode point = head;
        int length = 1;
        while (null != point.next) {
            point = point.next;
            length++;
        }
        if (n > length)
        {
            return head;
        }

        ListNode temp = head;
        if (length == n) {
            head = temp.next;
            temp.next = null;
            return head;
        }
        int i = 1;
        while (null != temp.next && i + n < length) {
            temp = temp.next;
            i++;
        }
        //此時temp處於倒數n+1個node上
        ListNode nthNode = temp.next;
        temp.next = nthNode.next;
        nthNode.next = null;
        return head;
    }
}


  • 刪除鏈表中相同的元素

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
public ListNode deleteDuplicates(ListNode head) {
    //只有一個節點或節點爲空
    if (null == head || null == head.next) {
        return head;
    }
    ListNode point = head;
    ListNode temp = null;
    int flag = 0;
    while (null != point && null != (temp = point.next)) {
        //前N個節點相同的情況
        while (null != temp && point.val == temp.val) {
            temp = temp.next;
            point = point.next;
            flag++;
        }
        if (0 != flag) {
            point.next = null;
            point = temp;
            head = point;  //表頭重新賦值
            flag = 0;
            continue;
        }
        while (null!= temp.next && temp.val == temp.next.val) {
            temp = temp.next;
            flag++;
        }
        if (0 != flag) {
            point.next = temp.next;
            temp.next = null;
            flag = 0;
            continue;   //鏈表重新組裝後需要再次進入循環
        }
        point = point.next;
    }
    return head;
}


  • Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes’ values.
For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.
public void reorderList(ListNode head) {
     //排除null,節點數爲12List
    if (null == head || null == head.next || null == head.next.next) {
        return;
    }

ListNode point = head;
    ListNode secondaryEnd = head.next;  //倒數第二個節點
    while (null != secondaryEnd.next && null != secondaryEnd.next.next) {
        secondaryEnd = secondaryEnd.next;
    }

    ListNode end = secondaryEnd.next;   //最後一個節點
    if (null != point.next) {
        secondaryEnd.next = null;
        end.next = point.next;
        point.next = end;
        point = end.next;
        reorderList(point);
    }
   }

這種解法的平均時間複雜度應該是O(n2) , 在LeetCode中報超時,暫時還未想到更快的辦法。

發佈了29 篇原創文章 · 獲贊 24 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章