【LeetCode】237. Delete Node in a Linked List
2023-09-11 14:20:27 时间
Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4
and you are given the third node with value 3
, the linked list should become 1 -> 2 -> 4
after calling your function.
后一个赋值给前一个,注意结尾的置空与删除。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void deleteNode(ListNode* node) { ListNode* cur = node; ListNode* post = cur->next; // except the tail, thus has access to the next while(true) { cur->val = post->val; if(post->next == NULL) { cur->next = NULL; return; } else { cur = post; post = post->next; } } } };
相关文章
- Leetcode: Insert Delete GetRandom O(1) - Duplicates allowed
- Leetcode: Delete Node in a Linked List
- Leetcode: Symmetric Tree
- Leetcode: Convert Sorted List to Binary Search Tree
- Leetcode: Remove Nth Node From End of List
- [leetcode] 242. Valid Anagram
- LeetCode高频题55. 跳跃游戏
- 【Leetcode】102. 二叉树的层序遍历(中等)
- 【Leetcode】547.省份数量(中等)
- LeetCode每日一练(10)
- [LeetCode] Linked List Cycle
- [LeetCode]258. 各位相加
- [LeetCode]5384. 拥有最多糖果的孩子
- 【算法/贪心】leetcode刷题路线(持续更新)
- leetcode竞赛记录-第64场双周赛
- 【LeetCode】82. Remove Duplicates from Sorted List II
- 【LeetCode】142. Linked List Cycle II (2 solutions)
- 【Leetcode】Linked List Cycle II
- LeetCode OJ平台Sort Colors讨论主题算法
- LeetCode——Remove Nth Node From End of List
- [LeetCode] 1171. Remove Zero Sum Consecutive Nodes from Linked List 从链表中删去总和值为零的连续节点
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
- [LeetCode] 364. Nested List Weight Sum II 嵌套链表权重和之二
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
- leetcode 279. Perfect Squares 完全平方数(中等)