如何用Python和C语言编写代码删除链表中的节点?
- 内容介绍
- 文章标签
- 相关推荐
本文共计630个文字,预计阅读时间需要3分钟。
给定向量链表的头指针和一个要删除的节点的值,定义一个函数删除该节点,并返回删除后的链表的头节点。
pythonclass ListNode: def __init__(self, value=0, next=None): self.value=value self.next=next
def delete_node(head, val): if not head: return None
if head.value==val: return head.next
current=head while current.next and current.next.value !=val: current=current.next
if current.next: current.next=current.next.next
return head
示例head=ListNode(4, ListNode(5, ListNode(1, ListNode(9))))val=5new_head=delete_node(head, val)while new_head: print(new_head.value, end= ) new_head=new_head.next
输出结果:
41 9
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
示例 1:
输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
示例 2:
输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
思路:
建立一个空节点作为哨兵节点,可以把首尾等特殊情况一般化,且方便返回结果,使用双指针将更加方便操作链表。
Python解法:
class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def deleteNode(self, head: ListNode, val: int) -> ListNode: tempHead = ListNode(None) # 构建哨兵节点 tempHead.next = head prePtr = tempHead # 使用双指针 postPtr = head while postPtr: if postPtr.val == val: prePtr.next = postPtr.next break prePtr = prePtr.next postPtr = postPtr.next return tempHead.next
C++解法:
struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: ListNode* deleteNode(ListNode* head, int val) { ListNode *tempHead = new ListNode(-1); // 哨兵节点,创建节点一定要用new!!!!!!!!!!!!!! tempHead->next = head; ListNode *prePtr = tempHead; ListNode *postPtr = head; while (postPtr) { if (postPtr->val == val) { prePtr->next = postPtr->next; // 画图确定指针指向关系,按照箭头确定指向 break; } postPtr = postPtr->next; prePtr = prePtr->next; } return tempHead->next; } };
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计630个文字,预计阅读时间需要3分钟。
给定向量链表的头指针和一个要删除的节点的值,定义一个函数删除该节点,并返回删除后的链表的头节点。
pythonclass ListNode: def __init__(self, value=0, next=None): self.value=value self.next=next
def delete_node(head, val): if not head: return None
if head.value==val: return head.next
current=head while current.next and current.next.value !=val: current=current.next
if current.next: current.next=current.next.next
return head
示例head=ListNode(4, ListNode(5, ListNode(1, ListNode(9))))val=5new_head=delete_node(head, val)while new_head: print(new_head.value, end= ) new_head=new_head.next
输出结果:
41 9
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
示例 1:
输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
示例 2:
输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
思路:
建立一个空节点作为哨兵节点,可以把首尾等特殊情况一般化,且方便返回结果,使用双指针将更加方便操作链表。
Python解法:
class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def deleteNode(self, head: ListNode, val: int) -> ListNode: tempHead = ListNode(None) # 构建哨兵节点 tempHead.next = head prePtr = tempHead # 使用双指针 postPtr = head while postPtr: if postPtr.val == val: prePtr.next = postPtr.next break prePtr = prePtr.next postPtr = postPtr.next return tempHead.next
C++解法:
struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: ListNode* deleteNode(ListNode* head, int val) { ListNode *tempHead = new ListNode(-1); // 哨兵节点,创建节点一定要用new!!!!!!!!!!!!!! tempHead->next = head; ListNode *prePtr = tempHead; ListNode *postPtr = head; while (postPtr) { if (postPtr->val == val) { prePtr->next = postPtr->next; // 画图确定指针指向关系,按照箭头确定指向 break; } postPtr = postPtr->next; prePtr = prePtr->next; } return tempHead->next; } };
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

