LeetCode 707. Design Linked List (设计链表)

题目标签:Linked List

  题目让我们自己设计一个 linked list,可以是单向和双向的。这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code。

 

Java Solution:

Runtime:  56 ms, faster than 21.21% 

Memory Usage: 45.2 MB, less than 88.89%

完成日期:07/08/2019

关键点:edge cases

复制代码
public class ListNode {
      int val;
      ListNode next;
    
      ListNode(int x) { 
          val = x;
      }
}

class MyLinkedList {
    
    ListNode dummyHead;
    /** Initialize your data structure here. */
    public MyLinkedList() {
        dummyHead = new ListNode(0);
        dummyHead.next = null;
    }
    
    /** 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 < 0)
            return -1;
        
        ListNode cursor = dummyHead.next;
        int i = 0;
        
        while(cursor != null) {
            if(i == index) 
                return cursor.val;
            
            i++;
            cursor = cursor.next;
        }
        
        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) {
        // case 1: no first node
        if(dummyHead.next == null) {
            ListNode node = new ListNode(val);
            dummyHead.next = node;
            node.next = null;
        }
        else {
            ListNode node = new ListNode(val);
            node.next = dummyHead.next;
            dummyHead.next = node;
        }
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        ListNode cursor = dummyHead;
        
        // find the node's next is null, insert node after that node
        while(cursor.next != null) { 
            cursor = cursor.next;
        }
        
        ListNode node = new ListNode(val);
        node.next = cursor.next;
        cursor.next = node;
    }
    
    /** 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;
        }
            
        ListNode cursor = dummyHead;
        ListNode prevNode = dummyHead;
        int i = -1;
        
        while(cursor != null) {
            // if find the spot to insert node
            if(i + 1 == index) {
                ListNode node = new ListNode(val);
                node.next = cursor.next;
                cursor.next = node;
                
                return;
            }
            
            i++;
            cursor = cursor.next;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if(index < 0)
            return;
        
        ListNode cursor = dummyHead.next;
        ListNode prevNode = dummyHead;
        int i = 0;
        
        while(cursor != null) {
            if(i == index) { // find the node to delete
                prevNode.next = cursor.next;
                cursor.next = null;
                
                return;
            }

            i++;
            prevNode = cursor;
            cursor = cursor.next;
        }
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */
复制代码

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

posted @   Jimmy_Cheng  阅读(234)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· MQ 如何保证数据一致性?
历史上的今天:
2017-09-09 LeetCode 268. Missing Number (缺失的数字)
2017-09-09 LeetCode 259. 3Sum Smaller (三数之和较小值) $
2017-09-09 LeetCode 245. Shortest Word Distance III (最短单词距离之三) $
2017-09-09 LeetCode 243. Shortest Word Distance (最短单词距离)$
点击右上角即可分享
微信分享提示