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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 马克思_单链表-Java

發布時間:2025/3/11 java 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 马克思_单链表-Java 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

public class SinglyListNode {

int val;

SinglyListNode next;

SinglyListNode() {

}

SinglyListNode(int x) {

this.val = x;

}

}

/*執行用時:

12 ms

, 在所有 Java 提交中擊敗了

66.93%

的用戶

內存消耗:

39.5 MB

, 在所有 Java 提交中擊敗了

5.06%

的用戶

*/

class MyLinkedList {

int size; // 0 -- n

//SinglyListNode head = new SinglyListNode(); // 此種寫法錯誤,鏈表遍歷時最后會輸出0

SinglyListNode head = null;

/** Initialize your data structure here. */

public MyLinkedList() {

}

/*public int length() {

int length = 0;

SinglyListNode p = head;

while (p != null) {

length ++;

p = p.next;

}

return length;

}*/

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

return -1;

}

SinglyListNode p;

p = head;

if (index != 0) {

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

p = p.next;

}

}

return p.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) {

SinglyListNode listNode = new SinglyListNode(val); // 創建加入的節結點

if (head != null) {

listNode.next = head;// 新節點的指針域指向當前鏈表的頭節點

}

head = listNode; // 將插入的節點作為新的頭節點

size ++;

}

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

public void addAtTail(int val) {

SinglyListNode listNode = new SinglyListNode(val);

if(head == null) {

head = listNode;

size ++;

// listNode.next = null; // 冗余

return;

}

SinglyListNode p = head;

/*while (p != null) {

if (p.next == null) {

p.next = listNode;

listNode.next = null;

p = null; //循環終止條件

}else {

p = p.next;

}

}*/

while (p.next != null) {

p = p.next; // 遍歷到尾節點位置

}

p.next =listNode;

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

addAtTail(val);

return;

}

if (index <= 0) {

addAtHead(val);

return;

}

if (index > size - 1) {

return;

}

SinglyListNode listNode = new SinglyListNode(val);

SinglyListNode p = head;

// 遍歷到節點要插入位置的前一個位置

while (index - 1 > 0) {

p = p.next;

index --;

}

listNode.next = p.next;

p.next = listNode;

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

return;

}

if (index == 0) {

head = head.next;

size --;

return;

}

SinglyListNode p = head;

SinglyListNode tempNode;

while (index - 1 > 0) {

p = p.next;

index --;

}

tempNode = p.next.next;

p.next = tempNode;

size --;

}

}

總結

以上是生活随笔為你收集整理的java 马克思_单链表-Java的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。