日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

力扣Java编译器_力扣--设计单链表

發(fā)布時(shí)間:2023/12/19 java 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 力扣Java编译器_力扣--设计单链表 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在鏈表類中實(shí)現(xiàn)這些功能:

get(index):獲取鏈表中第 index 個(gè)節(jié)點(diǎn)的值。如果索引無效,則返回-1。

addAtHead(val):在鏈表的第一個(gè)元素之前添加一個(gè)值為 val 的節(jié)點(diǎn)。插入后,新節(jié)點(diǎn)將成為鏈表的第一個(gè)節(jié)點(diǎn)。

addAtTail(val):將值為 val 的節(jié)點(diǎn)追加到鏈表的最后一個(gè)元素。

addAtIndex(index,val):在鏈表中的第 index 個(gè)節(jié)點(diǎn)之前添加值為 val 的節(jié)點(diǎn)。如果 index 等于鏈表的長(zhǎng)度,則該節(jié)點(diǎn)將附加到鏈表的末尾。如果 index 大于鏈表長(zhǎng)度,則不會(huì)插入節(jié)點(diǎn)。如果index小于0,則在頭部插入節(jié)點(diǎn)。

deleteAtIndex(index):如果索引 index 有效,則刪除鏈表中的第 index 個(gè)節(jié)點(diǎn)。

package com.lara.springbootconfig.algorithm;

/**

* @author lara

* @date 2020/3/20 17:05

*/

public class MyLinkedList {

private Node head;

private int size;

/**

* Initialize your data structure here.

*/

public MyLinkedList() {

}

/**

* 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 - 1) {

return -1;

}

Node temp = head;

for (int i = 0; i < index; i++) {

temp = temp.next;

}

return temp.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.

*/

public void addAtHead(int val) {

Node cur = new Node(val);

cur.next = head;

head = cur;

size++;

}

/**

* Append a node of value val to the last element of the linked list.

*/

public void addAtTail(int val) {

Node cur = new Node(val);

if (head == null) {

head = cur;

return;

}

Node temp = head;

while (null != temp.next) {

temp = temp.next;

}

temp.next = cur;

size++;

}

/**

* 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 > size) {

return;

}

if (index == 0) {

addAtHead(val);

size++;

return;

}

Node pre = head;

Node cur = pre.next;

for (int i = 1; i < index; i++) {

pre = pre.next;

cur = cur.next;

}

Node add = new Node(val);

add.next = cur;

pre.next = add;

size++;

}

/**

* Delete the index-th node in the linked list, if the index is valid.

*/

public void deleteAtIndex(int index) {

if (index >= 0 && index <= size - 1) {

if (index == 0) {

head = head.next;

size--;

return;

}

Node pre = head;

Node cur = pre.next;

for (int i = 1; i < index; i++) {

pre = pre.next;

cur = cur.next;

}

if (null == cur.next) {

pre.next = null;

} else {

pre.next = cur.next;

}

size--;

}

}

class Node {

private int val;

private Node next;

public Node(int val) {

this.val = val;

}

public int getVal() {

return val;

}

public void setVal(int val) {

this.val = val;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

public void print() {

for (int i = 0; i < this.size; i++) {

System.out.print(this.get(i));

}

System.out.println("--------------");

}

public static void main(String[] args) {

MyLinkedList obj = new MyLinkedList();

obj.addAtHead(1);

obj.print();

obj.addAtTail(3);

obj.print();

obj.addAtIndex(1, 2);

obj.print();

obj.deleteAtIndex(1);

obj.print();

}

}

慢慢長(zhǎng)路第一步,算法不能放棄!!!! 最好在力扣網(wǎng)提供的編輯器中自己手寫,嘗試下沒有編譯器看看自己還能不能寫代碼

總結(jié)

以上是生活随笔為你收集整理的力扣Java编译器_力扣--设计单链表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。