/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteListNode(ListNode* head) {
if(head==nullptr||head->next==nullptr)return head;
head->next=deleteListNode(head->next->next);
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteListNode(ListNode* head) {
if (head == NULL) return NULL;
for (ListNode *tmp = head; tmp != NULL; tmp = tmp -> next) {
if (tmp -> next != NULL) {
tmp -> next = tmp -> next -> next;
}
}
return head;
}
};
Java解法:
class Solution {
public ListNode deleteListNode(ListNode head) {
if (head != null && head.next != null) {
head.next = deleteListNode(head.next.next);
}
return head;
}
}