Leetcode203. 移除链表元素
题目链接:203. 移除链表元素
C++:
注意:C++需要delete删除的结点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode *dummy_head = new ListNode(0, head);
ListNode *p = dummy_head;
while(p->next != nullptr)
{
if(p->next->val == val)
{
ListNode *tmp = p->next;
p->next = p->next->next;
delete tmp;
}
else
p = p->next;
}
head = dummy_head->next;
delete dummy_head;
return head;
}
};
Python:
注意:python中定义虚拟头节点的方法,dummy_head.next = head是错误写法
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
dummy_head = ListNode(next = head)
cur = dummy_head
while cur.next != None:
if cur.next.val == val:
cur.next = cur.next.next
else:
cur = cur.next
return dummy_head.next
Leetcode707.设计链表
题目链接:707.设计链表
C++:
之前做过一次了,重新做一次又出现很多错误,也有了很多收获。
class MyLinkedList {
public:
//定义链表结构体
struct ListNode{
int val;
ListNode *next;
ListNode(int val):val(val), next(nullptr){}
};
MyLinkedList() {
dummyhead = new ListNode(0);
size = 0;
}
int get(int index) {
if(index >= size || index < 0)
return -1;
ListNode *cur = dummyhead->next;
if(size > 0)
{
while(index--)
cur = cur->next;
return cur->val;
}
else
return -1;
}
void addAtHead(int val) {
ListNode *p = new ListNode(val);
p->next = dummyhead->next;
dummyhead->next = p;
size++;
}
void addAtTail(int val) {
ListNode *p = new ListNode(val);
ListNode *cur = dummyhead;
while(cur->next != nullptr)
{
cur = cur->next;
}
cur->next = p;
size++;
}
void addAtIndex(int index, int val) {
ListNode *p = new ListNode(val);
ListNode *cur = dummyhead;
if(index <= size)
{
while(index--)
{
cur = cur->next;
}
if(cur->next != nullptr)
p->next = cur->next;
cur->next = p;size++;
}
}
void deleteAtIndex(int index) {
ListNode *cur = dummyhead;
if(index < size && index >= 0)
{
while(index--)
{
cur = cur->next;
}
ListNode *temp = cur->next;
cur->next = cur->next->next;
delete temp;
size--;
}
}
private:
ListNode *dummyhead;
int size;
};
Python:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class MyLinkedList:
def __init__(self):
self.dummyhead = ListNode()
self.size = 0
def get(self, index: int) -> int:
if index < self.size and index >= 0:
cur = self.dummyhead.next
while index:
cur = cur.next
index -= 1
return cur.val
else:
return -1
def addAtHead(self, val: int) -> None:
p = ListNode(val)
p.next = self.dummyhead.next
self.dummyhead.next = p
self.size += 1
def addAtTail(self, val: int) -> None:
p = ListNode(val)
cur = self.dummyhead
while cur.next != None:
cur = cur.next
cur.next = p
self.size += 1
def addAtIndex(self, index: int, val: int) -> None:
if index >= 0 and index <= self.size:
p = ListNode(val)
cur = self.dummyhead
while index:
cur = cur.next
index -= 1
p.next = cur.next
cur.next = p
self.size += 1
def deleteAtIndex(self, index: int) -> None:
if index < self.size and index >= 0:
cur = self.dummyhead
while index:
cur = cur.next
index -= 1
cur.next = cur.next.next
self.size -= 1
Leetcode206.反转链表
题目链接:206. 反转链表
总是出现访问空指针的错误
C++:(暴力解法)
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *cur = head;
ListNode *pre = nullptr;
while(cur != nullptr)
{
ListNode *temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};
Python:(递归)
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
return self.reverse(head, None)
def reverse(self, cur: ListNode, pre: ListNode) -> ListNode:
if cur == None:
return pre
temp = cur.next
cur.next = pre
return self.reverse(temp, cur)