代码随想录算法训练营第三天 | 203.移除链表元素、707.设计链表、206.反转链表
203.移除链表元素
代码
//leetcode submit region begin(Prohibit modification and deletion)
//*
// * Definition for singly-linked list.
// * public class ListNode {
// * int val;
// * ListNode next;
// * ListNode() {}
// * ListNode(int val) { this.val = val; }
// * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
// * }
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummy = new ListNode(-1,head);
ListNode pre = dummy;
ListNode cur = head;
/*
while (cur!=val){
pre=cur;
cur=cur.next;
if(cur.val=val){
// if(cur=val)
pre=cur.next;
cur=pre.next;
}
}
*/
while (cur!=null){
// 这个while语句里面往往是前提(不为空)
// 空指针直接就是cur=null,不是里面的值,即cul.val=null
// cur.val才是值了,注意!
if(cur.val==val){
// if里面应该是==的值等
pre.next=cur.next;
cur=cur.next;
}else {
pre=cur;
cur=cur.next;
}
}
return dummy.next;
}
}
//leetcode submit region end(Prohibit modification and deletion
题图
心得
// 这个while语句里面往往是前提(不为空)
// 空指针直接就是cur=null,不是里面的值,即cul.val=null
// cur.val才是值了,注意!
// if里面应该是==的值等
707.设计链表
代码(先码上)
//单链表
class ListNode {
int val;
ListNode next;
ListNode(){}
ListNode(int val) {
this.val=val;
}
}
class MyLinkedList {
//size存储链表元素的个数
int size;
//虚拟头结点
ListNode head;
//初始化链表
public MyLinkedList() {
size = 0;
head = new ListNode(0);
}
//获取第index个节点的数值,注意index是从0开始的,第0个节点就是头结点
public int get(int index) {
//如果index非法,返回-1
if (index < 0 || index >= size) {
return -1;
}
ListNode currentNode = head;
//包含一个虚拟头节点,所以查找第 index+1 个节点
for (int i = 0; i <= index; i++) {
currentNode = currentNode.next;
}
return currentNode.val;
}
//在链表最前面插入一个节点,等价于在第0个元素前添加
public void addAtHead(int val) {
addAtIndex(0, val);
}
//在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
public void addAtTail(int val) {
addAtIndex(size, val);
}
// 在第 index 个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
// 如果 index 等于链表的长度,则说明是新插入的节点为链表的尾结点
// 如果 index 大于链表的长度,则返回空
public void addAtIndex(int index, int val) {
if (index > size) {
return;
}
if (index < 0) {
index = 0;
}
size++;
//找到要插入节点的前驱
ListNode pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
ListNode toAdd = new ListNode(val);
toAdd.next = pred.next;
pred.next = toAdd;
}
//删除第index个节点
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}
size--;
if (index == 0) {
head = head.next;
return;
}
ListNode pred = head;
for (int i = 0; i < index ; i++) {
pred = pred.next;
}
pred.next = pred.next.next;
}
}
206.反转链表
代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre=null;
ListNode cur=head;
ListNode temp=null;
// ListNode temp;
// 用temp保存后结点而不是前结点(断链后无法找到)
while (cur!=null) {
// while (cur.next!=null)如果这样的话到4和5的时候,5就不会指向4
temp=cur.next;
cur.next=pre;
pre=cur;
cur=temp;
}
return pre;
// 又忘记返回值
}
}
//leetcode submit region end(Prohibit modification and deletion)
心得
- // 又忘记返回值
- // 用temp保存后结点而不是前结点(断链后无法找到)
- 不太容易想出来还是得画图,这样反而更节省时间