203.移除链表元素
203. 移除链表元素
https://leetcode.cn/problems/remove-linked-list-elements/
解题思路:
双指针的思想, 删除元素一定是用 前置节点去链接当前节点的next节点,
可以的思想有2个,一个是1、设置虚拟节点
ListNode virtualHead=new ListNode(-1,head);
一个是2、直接模拟,把所有前置节点为val值的情况都排除完
while(head!=null && head.val==val){
head=head.next;
}
后续二者是一样的处理
while(cur!=null){
if(cur.val==val){
pre.next=cur.next;
}else{
pre=cur;
}
cur=cur.next;
}
707. 设计链表
707. 设计链表https://leetcode.cn/problems/design-linked-list/
解题思路:
硬写......
206. 反转链表
206. 反转链表https://leetcode.cn/problems/reverse-linked-list/
1->2->3->4->5
1 <- 2 3->4->5
pre cur temp(cur.next)
1<-2<-3 4->5
如此循环
每次都要想一遍咋用三个节点,4个命令循环完的,这次也是...
while(cur!=null){
temp=cur.next;
cur.next=pre;
pre=cur;
cur=temp;
}