mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1642 from Tcotyledons/master
更新了707设计链表 java版本的 单链表删除的循环跳出条件和优化了双链表的插入元素的统一操作
This commit is contained in:
@ -358,7 +358,7 @@ class MyLinkedList {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ListNode pred = head;
|
ListNode pred = head;
|
||||||
for (int i = 0; i < index - 1; i++) {
|
for (int i = 0; i < index ; i++) {
|
||||||
pred = pred.next;
|
pred = pred.next;
|
||||||
}
|
}
|
||||||
pred.next = pred.next.next;
|
pred.next = pred.next.next;
|
||||||
@ -366,92 +366,101 @@ class MyLinkedList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//双链表
|
//双链表
|
||||||
class MyLinkedList {
|
class ListNode{
|
||||||
class ListNode {
|
int val;
|
||||||
int val;
|
ListNode next,prev;
|
||||||
ListNode next,prev;
|
ListNode() {};
|
||||||
ListNode(int x) {val = x;}
|
ListNode(int val){
|
||||||
|
this.val = val;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class MyLinkedList {
|
||||||
|
|
||||||
|
//记录链表中元素的数量
|
||||||
int size;
|
int size;
|
||||||
ListNode head,tail;//Sentinel node
|
//记录链表的虚拟头结点和尾结点
|
||||||
|
ListNode head,tail;
|
||||||
/** Initialize your data structure here. */
|
|
||||||
public MyLinkedList() {
|
public MyLinkedList() {
|
||||||
size = 0;
|
//初始化操作
|
||||||
head = new ListNode(0);
|
this.size = 0;
|
||||||
tail = new ListNode(0);
|
this.head = new ListNode(0);
|
||||||
head.next = tail;
|
this.tail = new ListNode(0);
|
||||||
tail.prev = head;
|
//这一步非常关键,否则在加入头结点的操作中会出现null.next的错误!!!
|
||||||
|
head.next=tail;
|
||||||
|
tail.prev=head;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
|
|
||||||
public int get(int index) {
|
public int get(int index) {
|
||||||
if(index < 0 || index >= size){return -1;}
|
//判断index是否有效
|
||||||
ListNode cur = head;
|
if(index<0 || index>=size){
|
||||||
|
return -1;
|
||||||
// 通过判断 index < (size - 1) / 2 来决定是从头结点还是尾节点遍历,提高效率
|
}
|
||||||
if(index < (size - 1) / 2){
|
ListNode cur = this.head;
|
||||||
for(int i = 0; i <= index; i++){
|
//判断是哪一边遍历时间更短
|
||||||
cur = cur.next;
|
if(index >= size / 2){
|
||||||
}
|
//tail开始
|
||||||
}else{
|
|
||||||
cur = tail;
|
cur = tail;
|
||||||
for(int i = 0; i <= size - index - 1; i++){
|
for(int i=0; i< size-index; i++){
|
||||||
cur = cur.prev;
|
cur = cur.prev;
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
for(int i=0; i<= index; i++){
|
||||||
|
cur = cur.next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return cur.val;
|
return cur.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) {
|
public void addAtHead(int val) {
|
||||||
ListNode cur = head;
|
//等价于在第0个元素前添加
|
||||||
ListNode newNode = new ListNode(val);
|
addAtIndex(0,val);
|
||||||
newNode.next = cur.next;
|
|
||||||
cur.next.prev = newNode;
|
|
||||||
cur.next = newNode;
|
|
||||||
newNode.prev = cur;
|
|
||||||
size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Append a node of value val to the last element of the linked list. */
|
|
||||||
public void addAtTail(int val) {
|
public void addAtTail(int val) {
|
||||||
ListNode cur = tail;
|
//等价于在最后一个元素(null)前添加
|
||||||
ListNode newNode = new ListNode(val);
|
addAtIndex(size,val);
|
||||||
newNode.next = tail;
|
|
||||||
newNode.prev = cur.prev;
|
|
||||||
cur.prev.next = newNode;
|
|
||||||
cur.prev = newNode;
|
|
||||||
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) {
|
public void addAtIndex(int index, int val) {
|
||||||
if(index > size){return;}
|
//index大于链表长度
|
||||||
if(index < 0){index = 0;}
|
if(index>size){
|
||||||
ListNode cur = head;
|
return;
|
||||||
for(int i = 0; i < index; i++){
|
}
|
||||||
cur = cur.next;
|
//index小于0
|
||||||
|
if(index<0){
|
||||||
|
index = 0;
|
||||||
}
|
}
|
||||||
ListNode newNode = new ListNode(val);
|
|
||||||
newNode.next = cur.next;
|
|
||||||
cur.next.prev = newNode;
|
|
||||||
newNode.prev = cur;
|
|
||||||
cur.next = newNode;
|
|
||||||
size++;
|
size++;
|
||||||
|
//找到前驱
|
||||||
|
ListNode pre = this.head;
|
||||||
|
for(int i=0; i<index; i++){
|
||||||
|
pre = pre.next;
|
||||||
|
}
|
||||||
|
//新建结点
|
||||||
|
ListNode newNode = new ListNode(val);
|
||||||
|
newNode.next = pre.next;
|
||||||
|
pre.next.prev = newNode;
|
||||||
|
newNode.prev = pre;
|
||||||
|
pre.next = newNode;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete the index-th node in the linked list, if the index is valid. */
|
|
||||||
public void deleteAtIndex(int index) {
|
public void deleteAtIndex(int index) {
|
||||||
if(index >= size || index < 0){return;}
|
//判断索引是否有效
|
||||||
ListNode cur = head;
|
if(index<0 || index>=size){
|
||||||
for(int i = 0; i < index; i++){
|
return;
|
||||||
cur = cur.next;
|
|
||||||
}
|
}
|
||||||
cur.next.next.prev = cur;
|
//删除操作
|
||||||
cur.next = cur.next.next;
|
|
||||||
size--;
|
size--;
|
||||||
|
ListNode pre = this.head;
|
||||||
|
for(int i=0; i<index; i++){
|
||||||
|
pre = pre.next;
|
||||||
|
}
|
||||||
|
pre.next.next.prev = pre;
|
||||||
|
pre.next = pre.next.next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user