移除链表元素
题目:LeetCode203
链接:https://leetcode.cn/problems/remove-linked-list-elements/
看到题目的第一反应就是将链表遍历一遍,在遍历的时候边做列表的删除操作就行,所以经过调试,写出了下面的代码
/**
* 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 l1 = new ListNode();
ListNode l2 = new ListNode();
l1 = null;
l2 = null;
if(head==null)return l2;
while(head.val==val){
head=head.next;
if(head==null){
return l2;
}
}
l1 = head;
while(l1.next!=null){
if(l1.next.val==val){
l2 = l1.next;
if(l2.next==null){
l1.next=null;
}
else{
l1.next = l2.next;
}
}
else{
l1 = l1.next;
}
if(l1==null)break;
}
return head;
}
}
看了题解后,发现还可以用递归的方法,自己在做题的时候总是想不到用递归的思路,还是对这方面掌握不足。代码如下:
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
head.next = removeElements(head.next, val);
return head.val == val ? head.next : head;
}
}
设计链表
题目:LeetCode707
链接:https://leetcode.cn/problems/design-linked-list/
看了题目之后第一眼的想法就是这个题是相对基础一些的,应该不难,就是细节注意一下就行了,相当于对之前学过的东西的巩固,不过也花了我挺多时间debug的,代码如下
class MyLinkedList {
int size;
ListNode node;
public MyLinkedList() {
size = 0;
node = new ListNode(0);
}
public int get(int index){
if(index<0||index>=size)return -1;
ListNode cur = node;
for(int i = 0;i < index;i++){
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
ListNode xin = new ListNode(val);
xin.next = node;
node = xin;
size++;
}
public void addAtTail(int val){
addAtIndex(size,val);
}
public void addAtIndex(int index, int val) {
if(index<0||index>size){
return;
}
size++;
ListNode cur = node;
if(index==0){
ListNode vv = new ListNode(val);
vv.next = node;
node = vv;
return;
}
for(int i = 0;i<index-1;i++){
cur = cur.next;
}
ListNode xin = new ListNode(val);
xin.next = cur.next;
cur.next = xin;
}
public void deleteAtIndex(int index){
if(index<0||index>=size)return;
size--;
ListNode aa = node;
if(index==0){
node = node.next;
return;
}
for(int i = 0;i<index-1;i++){
aa = aa.next;
}
aa.next = aa.next.next;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
看了题解之后,发现其实跟自己想的差不多,题解可能更取巧一点
反转链表
题目:LeetCode206
链接:https://leetcode.cn/problems/reverse-linked-list/submissions/
看到题目第一反应是可以创建一个栈把链表里的数据一个一个放进去再取出来,后面想了一下是不是占用的空间太多了,链表一长,时间也会长很多,实在想不到简单的方法,看了题解之后,发现自己之前确实想不到这样的办法,学习了之后把代码写了一遍,如下
/**
* 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 = new ListNode();
ListNode current = new ListNode();
ListNode hou = new ListNode();
if(head==null||head.next==null)return head;
hou = head.next;
pre = head;
pre.next=null;
while(hou!=null){
current = hou;
hou = hou.next;
current.next = pre;
pre = current;
}
return pre;
}
}
在网站看到一个思路觉得也应该学习学习,也是递归的思路
// 递归
class Solution {
public ListNode reverseList(ListNode head) {
return reverse(null, head);
}
private ListNode reverse(ListNode prev, ListNode cur) {
if (cur == null) {
return prev;
}
ListNode temp = null;
temp = cur.next;// 先保存下一个节点
cur.next = prev;// 反转
// 更新prev、cur位置
// prev = cur;
// cur = temp;
return reverse(cur, temp);
}
}
今日总结
值得注意的是,自己在写代码的时候往往就是把基本流程在脑子里过了一遍就开始写了,并没有注意边界处的判断,导致写的时候总是不断的修改,断断续续的debug,思路并不流畅完整,有些欠缺,希望后面能改善。