leetcode 19

指针方面的问题

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
            ListNode *The_nth_node,*next,*header_first;
            int count=1;
           // The_nth_node=(struct ListNode *)malloc(sizeof(ListNode));
           // The_nth_node=(struct ListNode *)malloc(sizeof(ListNode));
            header_first=head;
            The_nth_node=head;
            //next=(struct ListNode *)malloc(sizeof(ListNode));
            while(head->next!=NULL&&count<=n) {
                count++;
                head=head->next;
            }
            if(count==n) {
                return The_nth_node->next;
            }
            while(head->next!=NULL) {
                head=head->next;
                The_nth_node=The_nth_node->next;
            }
            next=The_nth_node->next;
            The_nth_node->next=next->next;
          // free(next);
            return header_first;
    }
};
时间: 2024-11-13 09:04:46

leetcode 19的相关文章

[leetcode] 19. Count and Say

这个还是一开始没读懂题目,题目如下: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1&qu

leetCode 19. Remove Nth Node From End of List 链表

19. 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 lis

Leetcode 19 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(19) - Remove Nth Node From End of List

题目要求是,给你一个单向链表,和一个数字n,删除该链表倒数第n个node,其中测试案例中n能保证一定有效. 思路很简单,用两个指针,它们两个相隔为n,当后面的指针指向链表尾的时候,前面的指针指向的node的下一个node,就是要删除的那一个. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int

LeetCode (19) Multiply Strings

题目描述 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 对两组非负数字进行相乘,使用数组表示数字,且题目中说明数组很大,因此,因此不能直接将数组转换成数字相乘.这道题目是要求自己构造乘法的思路,需要注意的地方主要为进位的处理. 解题

[LeetCode]#19 Remove Nth Node From the End of list

从移动回来一个月,就一直在忙项目申请书的事情.通过写申请书,看新闻联播的新闻稿都开始顺眼了. 师兄师姐们已经开始找工作了,我还有一年时间,工作方面早作准备.机器学习方面继续推导常见的算法,刷一刷kaggle,做做特征工程什么的,算是应用了. 今天这道题其实并不难,但我是在Linux下写的,有些本地调试的方法和技巧想记录一下. 一.题目 Given a linked list, remove the nth node from the end of list and return its head

LeetCode 19. 删除链表的倒数第N个节点(Remove Nth Node From End Of List)

题目描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 1->2->3->5. 说明: 给定的 n 保证是有效的. 进阶: 你能尝试使用一趟扫描实现吗? 解题思路 典型的利用双指针法解题.首先让指针first指向头节点,然后让其向后移动n步,接着让指针sec指向头结点,并和first一起向后移动.当first的next指针为NULL时,

leetcode 19: 删除链表的倒数第N个节点

题目:  给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 1->2->3->5. 1 ListNode* removeNthFromEnd(ListNode* head, int n) { 2 ListNode *p = head, *q = head; 3 while(n-- > 0 && nullptr != p

LeetCode 19. 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]19. Unique Paths唯一路径

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t