Leecode.203 移除链表元素
题目链接:移除链表元素
思路
移除链表元素关键在于前节点的next连接到当前节点的next上,还要注意头节点的情况,是否需要添加虚拟头节点,还要注意当删除节点之后,pre节点是不需要移动的,这里经常出错。
解题方法
三种实现方法,第一种是使用虚拟节点和pre节点,第二种是不使用虚拟节点但是使用pre节点,第三种是既不使用虚拟节点也不使用pre节点。
不使用虚拟节点的话,需要在开头就把head节点值等于val的情况全部移除掉,这样head节点就可以作为pre节点使用。
复杂度
- 时间复杂度:
O(n)
- 空间复杂度:
O(1)
Code
class Solution {
public ListNode removeElements(ListNode head, int val) {
//不添加虚拟节点,但是添加pre节点的方式
while(head != null && head.val == val){
head = head.next;
}
if(head == null){
return head;
}
ListNode pre = head;
ListNode curr = head.next;
while(curr != null){
if(curr.val == val){
pre.next = curr.next;
}else{
pre = pre.next;
}
curr = curr.next;
}
return head;
}
}
class Solution {
public ListNode removeElements(ListNode head, int val) {
//添加虚拟节点和pre节点的方式
if(head == null){
return head;
}
ListNode dummy = new ListNode(-1, head);
ListNode pre = dummy;
ListNode curr = head;
while(curr != null){
if(curr.val == val){
pre.next = curr.next;
}else{
pre = curr;
}
curr = curr.next;
}
return dummy.next;
}
}
class Solution {
public ListNode removeElements(ListNode head, int val) {
//不添加虚拟节点和pre节点的方式
while(head != null && head.val == val){
head = head.next;
}
ListNode curr = head;
while(curr != null){
while(curr.next != null && curr.next.val == val){
curr.next = curr.next.next;
}
curr = curr.next;
}
return head;
}
}
Leecode.707 设计链表
题目链接:设计链表
思路
链表的内部需要一个节点类,还需要一个头节点head和链表长度size。还有在addAtTail和addAtHead方法中,可以直接调用addAtIndex方法,注意判断index 的值是否超出了正常数值,在做添加和删除的同时记得要及时对size进行调整。
解题方法
同思路
复杂度
- 时间复杂度:
无
- 空间复杂度:
无
Code
class MyLinkedList {
class ListNode{
int val;
ListNode next;
ListNode(){};
ListNode(int val){
this.val = val;
}
}
ListNode head;
int size;
public MyLinkedList() {
size = 0;
head = new ListNode(0);
}
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.next.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 pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
ListNode toAdd = new ListNode(val);
toAdd.next = pred.next;
pred.next = toAdd;
}
public void deleteAtIndex(int index) {
if(index < 0 || index >= size){
return;
}
size--;
if (index == 0) {
head = head.next;
return;
}
ListNode pre = head;
for (int i = 0; i < index ; i++) {
pre = pre.next;
}
pre.next = pre.next.next;
}
}
Leecode.206 反转链表
题目链接:反转链表
思路
两种方法,迭代法和递归法。
解题方法
迭代法要用到temp、pre和curr,用temp去保存curr原本的next指向,因为待会就会改变curr的指向,不保存的话就会丢失,然后让curr的next指向pre,再pre移动一步变成curr,curr变成刚才的temp,这样完成两个节点之间的反转。
递归法比较抽象,分为从前向后递归和从后向前递归,本质是通过递归来代替迭代法最后的pre和curr的相互交换位置。
复杂度
- 时间复杂度:
双指针法为O(n),递归法为O(n)
- 空间复杂度:
双指针法为O(1),递归法为O(n)
Code
//双指针法
class Solution {
public ListNode reverseList(ListNode head) {
ListNode temp;
ListNode curr = head;
ListNode pre = null;
while(curr != null){
temp = curr.next;
curr.next = pre;
pre = curr;
curr = temp;
}
return pre;
}
}
//从前向后递归
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 = curr.next;
curr.next = pre;
return reverse(curr, temp);//递归法的这里就相当于代替了迭代法的curr和pre的交换位置
}
}
//从后向前递归
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode last = reverseList(head.next);//这里直接一步深入到链表的最后,然后下面依次进行交换。
head.next.next = head;
head.next = null;
return last;
}
}