leetcode[237]:Delete Node in a Linked List
2015-07-15 12:43
561 查看
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.
找不到前驱,可以删除其后继,其后继的值赋予当前结点即可。
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; * struct ListNode *next; * }; */ void deleteNode(struct ListNode* node) { struct ListNode *tmp; if(!node) return NULL; if(!node->next) node=NULL; tmp=node->next; node->val=node->next->val; node->next=node->next->next; free(tmp); }
找不到前驱,可以删除其后继,其后继的值赋予当前结点即可。
相关文章推荐
- JAVA LinkedList和ArrayList的使用及性能分析
- LinkedList学习示例模拟堆栈与队列数据结构
- ArrayList和LinkedList的主要区别
- ARRAYLIST VECTOR LINKEDLIST 区别与用法(2014-10-14)
- Remove Duplicates from Sorted List II
- Reverse Linked List
- Reverse Linked List II
- Partition List
- android性能之List
- Remove Nth Node From End of List
- Remove Duplicates from Sorted List
- Merge Two Sorted Lists
- ArrayList Vector LinkedList 区别与用法
- 黑马程序员————List集合概述、Arrayist集合、Vector集合、LinkedList集合简述
- 单向链表LinkedList的使用
- [Leetcode题解]002 Add Two Numbers
- LinkedList与ArrayList的区别
- Activity回退研究
- Java-Intersection of Two Linked Lists
- 黑马程序员_数组