1. 程式人生 > >Leetcode Linked List Problem 連結串列問題合集

Leetcode Linked List Problem 連結串列問題合集

1. Leet Code OJ 2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

您將獲得兩個非空連結列表,表示兩個非負整數。 數字以相反的順序儲存,並且它們的每個節點包含單個數字。 新增兩個數字並將其作為連結列表返回。

您可以假定這兩個數字不包含任何前導零,除了數字0本身。

輸入:(2→4→3)+(5→6→4)
輸出:7 - > 0 - > 8

程式碼:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode node = new ListNode(0); if(l1 == null && l2 == null) return l1; back(node, l1, l2); return node; } public void back(ListNode result, ListNode l1, ListNode l2){ if
(l1 != null) result.val += l1.val; else l1 = new ListNode(0); if(l2 != null) result.val += l2.val; else l2 = new ListNode(0); ListNode node = new ListNode(0); if(result.val >= 10){//說明會下一個節點值至少為1 result.val = result.val % 10; node.val = 1; result.next = node; } if( (l1.next != null || l2.next != null)){ result.next = node; back(result.next, l1.next, l2.next); } } }

2. Leet Code OJ 83. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

意思是:去除連結串列中重複的部分

分析

這裡需要注意的點:
1. 如何返回連結串列
2. 當連結串列中存在3個以上相同的節點時的處理

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null) return null;
        ListNode node = head;//1. 用一個節點來儲存鏈頭,用於返回
        while(head != null){
            ListNode next = head.next;
            if(next != null && next.val == head.val){
                head.next = next.next;
                continue;//2. 當有3個以上相同的節點時,不進入下一個節點,一直到相同節點的最後那個節點時,才跳到下一個節點
            }
            head = head.next;
        }
        return node;
    }
}

3. Min Stack

設計一個有最小值的Stack

分析

可以使用連結串列來構造

程式碼

public class MinStack {

    private Node head;

    public void push(int x) {
        if (head == null) {
            head = new Node(x, x);
        } else {
            head = new Node(x, Math.min(head.min, x), head);
        }
    }

    public void pop() {
        head = head.next;
    }

    public int top() {
        return head.val;
    }

    public int getMin() {
        return head.min;
    }

    private class Node {
        int val;
        int min;
        Node next;

        private Node(int val, int min) {
            this(val, min, null);
        }

        private Node(int val, int min, Node next) {
            this.val = val;
            this.min = min;
            this.next = next;
        }
    }
}