forked from youngyangyang04/leetcode-master
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from youngyangyang04:master #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Create 707设计链表java代码的修改
代码随想录网址上链表中的力扣707设计链表中的java代码:1,对于单链表的操作,删除元素的for循环跳出条件有误,进行了修改;2,对于双向链表,进行了插入头,尾元素和插入元素的统一操作。自己在原作者基础上写了一份设计链表的java代码。
- Loading branch information
commit ce5c5d010a778e9aadcacf903192b1da445206ff
There are no files selected for viewing
187 changes: 187 additions & 0 deletions
707设计链表java代码的修改
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
//原来的代码随想录707设计链表的java代码中单链表的设计中有错误,修正后并对双链表进行了简化和更正,下面是我的java代码 | ||
|
||
//单链表 | ||
class ListNode{ | ||
int val; | ||
ListNode next; | ||
ListNode() {}; | ||
ListNode(int val){ | ||
this.val = val; | ||
} | ||
} | ||
|
||
|
||
class MyLinkedList { | ||
|
||
//记录链表中元素的数量 | ||
int size; | ||
//记录链表的虚拟头结点 | ||
ListNode head; | ||
|
||
public MyLinkedList() { | ||
//初始化操作 | ||
this.size = 0; | ||
this.head = new ListNode(0); | ||
this.head.next = null; | ||
} | ||
|
||
public int get(int index) { | ||
//判断index是否有效 | ||
if(index<0 || index>=size){ | ||
return -1; | ||
} | ||
ListNode cur = this.head; | ||
//index 是可以取到的,因为有虚拟头结点 | ||
for(int i=0; i<= index; i++){ | ||
cur = cur.next; | ||
} | ||
return cur.val; | ||
} | ||
|
||
public void addAtHead(int val) { | ||
//等价于在第0个元素前添加 | ||
addAtIndex(0,val); | ||
} | ||
|
||
public void addAtTail(int val) { | ||
//等价于在最后一个元素(null)前添加 | ||
addAtIndex(size,val); | ||
} | ||
|
||
public void addAtIndex(int index, int val) { | ||
//index大于链表长度 | ||
if(index>size){ | ||
return; | ||
} | ||
//index小于0 | ||
if(index<0){ | ||
index = 0; | ||
} | ||
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 = newNode; | ||
} | ||
|
||
public void deleteAtIndex(int index) { | ||
//判断索引是否有效 | ||
if(index<0 || index>=size){ | ||
return; | ||
} | ||
//删除操作 | ||
size--; | ||
ListNode pre = this.head; | ||
for(int i=0; i<index; i++){ | ||
pre = pre.next; | ||
} | ||
pre.next = pre.next.next; | ||
|
||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------------------------------------------------------- | ||
//双向链表 | ||
class ListNode{ | ||
int val; | ||
ListNode next,prev; | ||
ListNode() {}; | ||
ListNode(int val){ | ||
this.val = val; | ||
} | ||
} | ||
|
||
|
||
class MyLinkedList { | ||
|
||
//记录链表中元素的数量 | ||
int size; | ||
//记录链表的虚拟头结点和尾结点 | ||
ListNode head,tail; | ||
|
||
public MyLinkedList() { | ||
//初始化操作 | ||
this.size = 0; | ||
this.head = new ListNode(0); | ||
this.tail = new ListNode(0); | ||
//这一步非常关键,否则在加入头结点的操作中会出现null.next的错误!!! | ||
head.next=tail; | ||
tail.prev=head; | ||
} | ||
|
||
public int get(int index) { | ||
//判断index是否有效 | ||
if(index<0 || index>=size){ | ||
return -1; | ||
} | ||
ListNode cur = this.head; | ||
//判断是哪一边遍历时间更短 | ||
if(index >= size / 2){ | ||
//tail开始 | ||
cur = tail; | ||
for(int i=0; i< size-index; i++){ | ||
cur = cur.prev; | ||
} | ||
}else{ | ||
for(int i=0; i<= index; i++){ | ||
cur = cur.next; | ||
} | ||
} | ||
return cur.val; | ||
} | ||
|
||
public void addAtHead(int val) { | ||
//等价于在第0个元素前添加 | ||
addAtIndex(0,val); | ||
} | ||
|
||
public void addAtTail(int val) { | ||
//等价于在最后一个元素(null)前添加 | ||
addAtIndex(size,val); | ||
} | ||
|
||
public void addAtIndex(int index, int val) { | ||
//index大于链表长度 | ||
if(index>size){ | ||
return; | ||
} | ||
//index小于0 | ||
if(index<0){ | ||
index = 0; | ||
} | ||
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; | ||
|
||
} | ||
|
||
public void deleteAtIndex(int index) { | ||
//判断索引是否有效 | ||
if(index<0 || index>=size){ | ||
return; | ||
} | ||
//删除操作 | ||
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; | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.