文章目录
Leetcode 203.移除链表元素
题目: 203.移除链表元素
解析: 2.5 练习时长两年半
解题思路
使用虚拟头节点,判定下一个节点是否为val,若为val,则删除下一个节点
代码
java
/**
* 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 cur = dummy;
while (cur.next != null){
if (cur.next.val == val){
cur.next = cur.next.next;
}else{
cur = cur.next;
}
}
return dummy.next;
}
}
总结
没试过不适用虚拟头节点
java
/**
* 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) {
while (head != null && head.val == val){
head = head.next;
}
if (head == null)
return head;
ListNode cur = head;
while (cur.next != null){
if (cur.next.val == val)
cur.next = cur.next.next;
else
cur = cur.next;
}
return head;
}
}
Leetcode 707.设计链表
题目: 707.设计链表
解析: 2.5 练习时长两年半
解题思路
正常思路
代码
java
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 MyLinkedList {
int size;
ListNode head;
public MyLinkedList() {
size = 0;
head = new ListNode(0);
}
public int get(int index) {
if (index < 0 || index >= size)
return -1;
ListNode curNode = head.next;
for (int i = 0; i < index; i++)
curNode = curNode.next;
return curNode.val;
}
public void addAtHead(int val) {
addAtIndex(0, val);
}
public void addAtTail(int val) {
addAtIndex(size, val);
}
public void addAtIndex(int index, int val) {
if (index > size)
return;
if (index < 0)
index = 0;
ListNode pre = head;
for(int i = 0; i < index; i++)
pre = pre.next;
ListNode toAdd = new ListNode(val);
toAdd.next = pre.next;
pre.next = toAdd;
size++;
}
public void deleteAtIndex(int index) {
if (index < 0 || index >= size)
return;
ListNode pre = head;
for(int i = 0; i < index; i++)
pre = pre.next;
pre.next = pre.next.next;
size--;
}
}
/**
* 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);
*/
总结
出现了奇怪的bug,全部删掉重新写一遍,没有bug了。
Leetcode 206. 反转链表
题目: 206. 反转链表
解析: 2.5 练习时长两年半
解题思路
头插法翻转链表
代码
java
/**
* 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 dummy = new ListNode(-1);
dummy.next = head;
ListNode cur = head;
while (cur != null && cur.next != null){
ListNode tmp = cur.next;
cur.next = tmp.next;
tmp.next = dummy.next;
dummy.next = tmp;
}
return dummy.next;
}
}
总结
暂无