【Leetcode】【Easy】Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

普通青年解法:

设置三个指针A\B\C,指针A和B间隔n-1个结点,用指针A去遍历链表直到指向最后一个元素,则此时指针B指向的结点为要删除的结点,指针C指向指针B的pre,方便删除的操作。

代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *removeNthFromEnd(ListNode *head, int n) {
12         ListNode *target = head;
13         ListNode *target_pre = NULL;
14         ListNode *findthelast = head;
15
16         if (head == NULL)
17             return head;
18
19         while(--n>0)
20             findthelast = findthelast->next;
21
22         while (findthelast->next) {
23             target_pre = target;
24             target = target->next;
25             findthelast = findthelast->next;
26         }
27
28         if (target_pre == NULL) {
29             head = target->next;
30             return head;
31         }
32
33         target_pre->next = target->next;
34
35         return head;
36     }
37 };

文艺智慧青年解法:

 1 class Solution
 2 {
 3 public:
 4     ListNode* removeNthFromEnd(ListNode* head, int n)
 5     {
 6         ListNode** t1 = &head, *t2 = head;
 7         for(int i = 1; i < n; ++i)
 8         {
 9             t2 = t2->next;
10         }
11         while(t2->next != NULL)
12         {
13             t1 = &((*t1)->next);
14             t2 = t2->next;
15         }
16         *t1 = (*t1)->next;
17         return head;
18     }
19 };
时间: 2024-10-10 12:36:59

【Leetcode】【Easy】Remove Nth Node From End of List的相关文章

LeetCode OJ 292.Nim Gam19. Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

【leetcode刷题笔记】Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

【LeetCode】Remove Nth Node From End of List (2 solutions)

Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题解:水题不解释 1 /** 2 * Definition for singly-linked list. 3 * str

【leetcode刷题笔记】Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of

【leetcode刷题笔记】Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上

【leetcode刷题笔记】Subsets

Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1