Leetcode 203 移除链表元素
题目链接:203. 移除链表元素 - 力扣(LeetCode)
/**
* 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 = head;
while(cur != null && cur.next != null) {
if(cur.next.val == val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return head;
}
}
/**
* 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);
dummy.next = head;
ListNode cur = dummy;
while(cur != null && cur.next != null) {
if(cur.next.val == val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return dummy.next;
}
}
Leetcode 707 设计链表
题目链接:707. 设计链表 - 力扣(LeetCode)
//创建一个 Node 类
class Node {
public int val;
public Node next;
public Node(int val) {
this.val = val;
}
}
class MyLinkedList {
public int size;
public Node head;
public MyLinkedList() {
this.size = 0;
//创建一个虚拟头节点
this.head = new Node(-1);
}
public int get(int index) {
if(index < 0 || index >= this.size) {
return -1;
}
Node cur = this.head;
for(int i = 0; i <= index; i++) {
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
addAtIndex(0, val);
}
public void addAtTail(int val) {
addAtIndex(this.size, val);
}
public void addAtIndex(int index, int val) {
if(index > this.size) {
return;
}
if(index < 0) {
index = 0;
}
this.size++;
Node cur = this.head;
// 找到 index - 1 节点
for(int i = 0; i < index; i++) {
cur = cur.next;
}
//构建将要插入的节点
Node node = new Node(val);
node.next = cur.next;
cur.next = node;
}
public void deleteAtIndex(int index) {
if(index < 0 || index >= this.size) {
return;
}
this.size--;
if(index == 0) {
this.head = this.head.next;
return;
}
Node cur = this.head;
for(int i = 0; i < index; i++) {
cur = cur.next;
}
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);
*/
Leetcode 206 反转链表
题目链接:206. 反转链表 - 力扣(LeetCode)
/**
* 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) {
//判断头节点是否为空
if(head == null) {
return head;
}
ListNode pre = head;
ListNode cur = null;
while(pre != null) {
ListNode next = pre.next;
pre.next = cur;
cur = pre;
pre = next;
}
return cur;
}
}