203.移除链表元素
- 题目描述:删除链表中等于给定值 val 的所有节点。链表有可能为空。
- 思路
第一种:头节点和非头节点分别考虑
第二种:加入虚拟头节点
//第一种
/*
* @lc app=leetcode.cn id=203 lang=java
* @lcpr version=21917
*
* [203] 移除链表元素
*/
// @lc code=start
/**
* 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;
}
ListNode cur = new ListNode();
cur.next = head;
while(cur.next != null){
if(cur.next.val == val){
cur.next = cur.next.next;
}else{
cur = cur.next;
}
}
return head;
}
}
// @lc code=end
/*
// @lcpr case=start
// [1,2,6,3,4,5,6]\n6\n
// @lcpr case=end
// @lcpr case=start
// []\n1\n
// @lcpr case=end
// @lcpr case=start
// [7,7,7,7]\n7\n
// @lcpr case=end
*/
//第二种
/*
* @lc app=leetcode.cn id=203 lang=java
* @lcpr version=21917
*
* [203] 移除链表元素
*/
// @lc code=start
/**
* 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 dummyHead = new ListNode();
dummyHead.next = head;
ListNode cur = new ListNode();
cur.next = dummyHead;
while(cur.next != null){
if(cur.next.val == val){
cur.next = cur.next.next;
}else{
cur = cur.next;
}
}
return dummyHead.next;
}
}
// @lc code=end
/*
// @lcpr case=start
// [1,2,6,3,4,5,6]\n6\n
// @lcpr case=end
// @lcpr case=start
// []\n1\n
// @lcpr case=end
// @lcpr case=start
// [7,7,7,7]\n7\n
// @lcpr case=end
*/
707.设计链表
- 题目描述:在链表类中实现这些功能:
1、get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
2、addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
3、addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
4、addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
5、deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
- 注意点
1、元素的增加和删除要在操作前对元素个数进行++/–
2、head作为虚拟头节点,后面跟首节点
//链表节点
class ListNode{
int val;
ListNode next;
ListNode(){}
ListNode(int val){
this.val = val;
}
}
class MyLinkedList {
//链表元素个数
int size;
//虚拟头节点
ListNode head;
public MyLinkedList() {
size = 0;
head = new ListNode();
}
public int get(int index) {
if(index < 0 || index > size - 1){
return -1;
}
ListNode cur = head;
while(index >= 0){
cur = cur.next;
index--;
}
return cur.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;
}
//增加一个元素,先增加元素个数
size++;
ListNode cur = head;
while(index > 0){
cur = cur.next;
index--;
}
ListNode newNode = new ListNode(val);
newNode.next = cur.next;
cur.next = newNode;
}
public void deleteAtIndex(int index) {
if(index < 0 || index > size - 1){
return;
}
//删除一个元素,先减少元素个数
size--;
ListNode cur = head;
while(index > 0){
cur = cur.next;
index--;
}
cur.next = cur.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);
*/
206.反转链表
-
题目描述:反转一个单链表。
-
代码随想录视频链接:https://www.bilibili.com/video/BV1nB4y1i7eL/?vd_source=110bab21cbd93149da7eacba5f850b6b
/**
* 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 cur = head;
ListNode pre = null;
while(cur != null){
ListNode tmp = cur.next;
cur.next = pre;
//指针翻转后,pre和cur后移
pre = cur;
cur = tmp;
}
return pre;
}
}
class Solution {
public ListNode reverse(ListNode cur, ListNode pre){
if(cur == null){
return pre;
}
ListNode tmp = cur.next;
cur.next = pre;
return reverse(tmp, cur);
}
public ListNode reverseList(ListNode head) {
//递归法
return reverse(head, null);
}
}