链表排序和自制链表148_707

148. Sort List

将一个链表排序。

  1. 用归并排序快一点,时间复杂度O(nlgn)
  2. 每次递归用快慢指针找到中点,然后将链表分成两个基本等长的链表
  3. 将两个链表merge
public ListNode sortList(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode fast = head.next, slow = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    ListNode l2 = slow.next;
    slow.next = null;

    return merge(sortList(head), sortList(l2));
}

public ListNode merge(ListNode l1, ListNode l2) {
    ListNode res = new ListNode(0);
    ListNode tail = res;
    while (l1 != null && l2 != null) {
        if (l1.val <= l2.val) {
            tail.next = l1;
            l1 = l1.next;
        } else {
            tail.next = l2;
            l2 = l2.next;
        }
        tail = tail.next;
        tail.next = null;
    }
    if (l1 != null) tail.next = l1;
    if (l2 != null) tail.next = l2;
    return res.next;
}

707. Design Linked List

自制一个链表,实现这些功能:

  1. get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1
  2. addAtHead(val) : Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
  3. addAtTail(val) : Append a node of value val to the last element of the linked list.
  4. addAtIndex(index, val) : Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
  5. deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.
  • 其实很简单,建一个普通结点就可以,然后全局作用域声明一个结点和一个整数分别表示链表头结点和长度
class MyLinkedList {

    /** Initialize your data structure here. */
    public class node {
        int val;
        node next;
        node(int num) {val = num;}
    }
    node head;
    int size;

    public MyLinkedList() {
        head = null;
        size = 0;
    }

    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
        if (index < size) {
            node temp = head;
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
            return temp.val;
        }
        return -1;
    }

    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void addAtHead(int val) {
        node newhead = new node(val);
        newhead.next = head;
        size++;
        head = newhead;
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        node tail = head;
        while (tail.next != null)
            tail = tail.next;

        size++;
        tail.next = new node(val);
    }

    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
        if (index == 0) {
            addAtHead(val);
            return;
        }
        
        node insertPoint = head;
        for (int i = 0; i < index-1; i++) {
            insertPoint = insertPoint.next;
        }
        node temp = insertPoint.next;
        insertPoint.next = new node(val);
        insertPoint.next.next = temp;
        size++;
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if (index < size) {
            if (index == 0) {
                head = head.next;
                return;
            }
            node deletePoint = head;
            for (int i = 0; i < index-1; i++) {
                deletePoint = deletePoint.next;
            }
            deletePoint.next = deletePoint.next.next;
            size--;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值