Lc707_MyLinkedList
本题 需要注意点就是边界问题
还有节点替换的时候注意保留指向的下一个节点保存
1package ch2;
2
3/**
4 * 设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
5 * <p>
6 * 在链表类中实现这些功能:
7 * <p>
8 * get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
9 * addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
10 * addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
11 * addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
12 * deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
13 *
14 * <p>
15 * 示例:
16 * <p>
17 * MyLinkedList linkedList = new MyLinkedList();
18 * linkedList.addAtHead(1);
19 * linkedList.addAtTail(3);
20 * linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
21 * linkedList.get(1); //返回2
22 * linkedList.deleteAtIndex(1); //现在链表是1-> 3
23 * linkedList.get(1); //返回3
24 *
25 * <p>
26 * 提示:
27 * <p>
28 * 所有val值都在 [1, 1000] 之内。
29 * 操作次数将在 [1, 1000] 之内。
30 * 请不要使用内置的 LinkedList 库。
31 * <p>
32 * 来源:力扣(LeetCode)
33 * 链接:https://leetcode-cn.com/problems/design-linked-list
34 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
35 */
36public class MyLinkedList {
37
38 private static class ListNode {
39 int val;
40 ListNode next;
41
42 ListNode() {
43 }
44
45 ListNode(int val) {
46 this.val = val;
47 }
48
49 ListNode(int val, ListNode next) {
50 this.val = val;
51 this.next = next;
52 }
53
54
55 }
56
57 int size;
58 ListNode sentinel;
59
60 /**
61 * Initialize your data structure here.
62 */
63 public MyLinkedList() {
64 sentinel = new ListNode();
65 size = 0;
66 }
67
68 /**
69 * Get the value of the index-th node in the linked list. If the index is invalid, return -1.
70 * * get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
71 */
72 public int get(int index) {
73 if (index >= size || index < 0) {
74 return -1;
75 }
76 ListNode curr = sentinel.next;
77 while ((index--) > 0) {
78 curr = curr.next;
79 }
80 return curr.val;
81 }
82
83 /**
84 * 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.
85 * * addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
86 */
87 public void addAtHead(int val) {
88 ListNode head = new ListNode(val);
89 head.next = sentinel.next;
90 sentinel.next = head;
91 size++;
92
93 }
94
95 /**
96 * Append a node of value val to the last element of the linked list.
97 * * addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
98 */
99 public void addAtTail(int val) {
100 ListNode newNode = new ListNode(val);
101 ListNode curr = sentinel;
102 while (curr.next != null) {
103 curr = curr.next;
104 }
105 curr.next = newNode;
106 size++;
107 }
108
109 /**
110 * 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.
111 * * addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
112 */
113 public void addAtIndex(int index, int val) {
114 if (index > size) {
115 return;
116 }
117
118 ListNode newNode = new ListNode(val);
119 ListNode curr = sentinel;
120 while ((index--) > 0) {
121 curr = curr.next;
122 }
123 newNode.next = curr.next;
124 curr.next = newNode;
125 size++;
126
127 }
128
129 /**
130 * Delete the index-th node in the linked list, if the index is valid.
131 * * deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
132 */
133 public void deleteAtIndex(int index) {
134 if (index >= size || index < 0) {
135 return;
136 }
137
138 ListNode curr = sentinel;
139 while ((index--) > 0) {
140 curr = curr.next;
141 }
142 curr.next = curr.next.next;
143 size--;
144 }
145
146 public static void main(String[] args) {
147 MyLinkedList obj = new MyLinkedList();
148 obj.addAtHead(1);
149 obj.addAtTail(3);
150 obj.addAtIndex(1, 2);
151 int param_1 = obj.get(1);
152 obj.deleteAtIndex(1);
153 int param_2 = obj.get(1);
154
155 }
156
157}
不恋尘世浮华,不写红尘纷扰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理