代码随想录算法训练营第三天| 203.移除链表元素、707.设计链表、206.反转链表
203.移除链表元素
/**
* 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 dummyNode = new ListNode();
dummyNode.next = head;
ListNode currentNode = dummyNode;
while(currentNode.next != null)
{
if(currentNode.next.val == val)
{
currentNode.next = currentNode.next.next;
}
else
{
currentNode = currentNode.next;
}
}
return dummyNode.next;
}
}
707. 设计链表
- 较难,需要多次复习
class MyLinkedList {
class ListNode
{
int val;
ListNode next;
ListNode(int val)
{
this.val = val;
}
}
//储存链表元素个数
private int size;
//定义虚拟头节点,并非真正的头节点
private ListNode head;
//链表初始化
public MyLinkedList() {
this.size = 0;
this.head = new ListNode(0);
}
//获得链表第index个节点的值
public int get(int index) {
if(index < 0 || index >= size)
{
return -1;
}
ListNode curr = head;
for(int i = 0; i <= index; i++)
{
curr = curr.next;
}
return curr.val;
}
public void addAtHead(int val) {
ListNode dummy = new ListNode(val);
dummy.next = head.next;
head.next = dummy;
size++;
}
public void addAtTail(int val) {
ListNode curr = head;
ListNode dummy = new ListNode(val);
while(curr.next != null)
{
curr = curr.next;
}
curr.next = dummy;
size++;
}
public void addAtIndex(int index, int val) {
if(index < 0 || index > size)
{
return;
}
ListNode curr = head;
ListNode dummy = new ListNode(val);
for(int i = 0; i < index; i++)
{
curr = curr.next;
}
dummy.next = curr.next;
curr.next = dummy;
size++;
}
public void deleteAtIndex(int index) {
if(index < 0 || index >= size)
{
return ;
}
ListNode curr = head;
for(int i = 0; i < index; i++)
{
curr = curr.next;
}
curr.next = curr.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);
*/
206. 反转链表
- 递归写法
/**
* 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) {
return reverse(null, head);
}
public ListNode reverse(ListNode pre, ListNode curr)
{
if(curr == null)
{
return pre;
}
ListNode temp = null;
temp = curr.next;
curr.next = pre;
return reverse(curr, temp);
}
}
- 双指针写法
/**
* 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 curr = head;
ListNode pre = null;
ListNode temp = null;
while(curr != null)
{
temp = curr.next;
curr.next = pre;
pre = curr;
curr = temp;
}
return pre;
}
}