代码随想录Day3 :链表part1

目录

LeetCode203:移除链表元素

题目描述:

​编辑

解题思路:

1.虚拟头节点:

原题链接:203. 移除链表元素 - 力扣(LeetCode)

LeetCode707:设计链表

题目描述:

解题思路:

1.虚拟头节点:

原题链接:707. 设计链表 - 力扣(LeetCode)

LeetCode206:反转链表

题目描述:

解题思路:

1.双指针:

2.递归:

3.使用栈:

原题链接:206. 反转链表 - 力扣(LeetCode)


LeetCode203:移除链表元素

题目描述:

解题思路:

1.使用虚拟头节点 :单向链表,删除一个节点,需要获取前一个节点。

1.虚拟头节点:

删除节点:前一个节点的指针指向 下下个节点 即可 (头节点没有上一个节点 ,所以使用虚拟头节点)

public class Solution {
    public ListNode RemoveElements(ListNode head, int val) {
        ListNode dammyhead =  new ListNode(0,head);
        ListNode cur = dammyhead;
        while(cur.next!=null) 
        {
            if(cur.next.val==val)
            {
                cur.next = cur.next.next;
            }
            else
            {
                cur = cur.next;
            }
        }
        return dammyhead.next;//因为head 可能被删掉,所以返回虚拟头节点的下一个
    }
}

原题链接:203. 移除链表元素 - 力扣(LeetCode)

LeetCode707:设计链表

题目描述:

解题思路:

本体考察,链表基础知识,使用虚拟头节点来实现

1.虚拟头节点:

1.获取第n个节点的值 ,设临时节点是虚拟头节点的下一个 ,因为索引从零开始

2.删除节点和在第n个节点加入节点 都要获取第n个节点的前一个节点进行操作

public class ListNode
{
    public int val ;
    public ListNode next;
    public ListNode(int val =0,ListNode next =null)
    {
        this.val =val;
        this.next = next;
    }
}

public class MyLinkedList {

    public int size;
    public ListNode dammyhead;

    public MyLinkedList() {
        size = 0;
        dammyhead = new ListNode(0,null);
    }
    
    public int Get(int index) {
        if(index<0||index>=size)
        return -1;
        ListNode cur = dammyhead.next;
        
             for(int i =0;i<index;i++)
            {
                cur = cur.next;
            }
        return cur.val;
    }
    
    public void AddAtHead(int val) {
        ListNode newNode = new ListNode(val,null);
        ListNode cur = dammyhead;
        newNode.next = cur.next;
        dammyhead.next = newNode;
        size++;
    }
    
    public void AddAtTail(int val) {
         ListNode newNode = new ListNode(val,null);
         ListNode cur = dammyhead;
         while(cur.next!=null)
         {
             cur =cur.next;
         }
         cur.next = newNode;
         size++;
    }
    
    public void AddAtIndex(int index, int val) {
        if(index>size)
        return ;
        if(index<0)
        index =0;
        
         ListNode newNode = new ListNode(val,null);
         ListNode cur =  dammyhead;
             for(int i =0;i<index;i++)
            {
                cur = cur.next;
            }
        newNode.next = cur.next;
        cur.next =newNode;
        size++;
    }
    
    public void DeleteAtIndex(int index) {
        if(index<0||index>=size)
            return ;

            ListNode cur = dammyhead;
             for(int i =0;i<index;i++)
            {
                cur = cur.next;
            }
            cur.next = cur.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);
 */

原题链接:707. 设计链表 - 力扣(LeetCode)

LeetCode206:反转链表

题目描述:

解题思路:

1.双指针:

指针cur 指向头节点,指针pre指向头节点的前一个节点 即空节点NULL,用临时节点temp暂存 cur.next节点,然后改变next指针指向,然后cur,pre向后移动,直至cur指向NULL。

public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur= head;
        while(cur!=null)
        {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}
2.递归:

与双指针思路一致,注意递归是传入的参数

public class Solution {
    public ListNode ReverseList(ListNode head) {
        return Reverse(head,null);
    }

    public ListNode Reverse (ListNode cur,ListNode pre)
    {
        if(cur==null)
        return pre;
        ListNode temp = cur.next;
        cur.next =pre;
        return  Reverse(temp,cur);
    }
}
3.使用栈:

根据栈先进后出的性质来反转链表,注意最后的节点 next要指向null,防止成环

public class Solution {
    public ListNode ReverseList(ListNode head) {
                Stack<ListNode> listNodes = new Stack<ListNode>();
        ListNode headNode = new ListNode(0,head); 
        while(head != null) 
        {
            listNodes.Push(head);
            head = head.next;
        }
        ListNode node = new ListNode(0,headNode);
        while(listNodes.Count > 0) 
        {
            headNode.next = listNodes.Pop();
            headNode = headNode.next;
        }
        headNode.next =null;
        return node.next.next;
    }
}
原题链接:206. 反转链表 - 力扣(LeetCode)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值