目录
原题链接:203. 移除链表元素 - 力扣(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;
}
}