编程练习:链表习题(下)

(6)convert-sorted-list-to-binary-search-tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

思路:
(1)分析题意:要求将一条有序链表转化为一颗二叉搜索树
(2)快慢指针法找到链表的中点
(3)以中点的值建立一个树的根节点
(4)从中间节点将原链表分为两条子链表
(5)分别对两个链表递归调用原函数

代码实现:

class Solution {
public:
    TreeNode *sortedListToBST(ListNode *head) {
        if(head == NULL)
            return NULL;
        ListNode* fast = head;
        ListNode* slow = head;
        ListNode* ret = NULL;
        while(fast!=NULL&&fast->next!=NULL)
        {
            fast = fast->next->next;
            ret = slow;
            slow = slow->next;
        }
        TreeNode* root = new TreeNode(slow->val);//找到中间节点作为根节点
        if(ret != NULL)
        {
            ret->next = NULL;
            root->left = sortedListToBST(head);
        }
        if(slow->next != NULL)
        {
            root->right = sortedListToBST(slow->next);
        }
        return root;
    }
};

(7)You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

翻译:
给出两个表示两个非负数的链表。这些数字以相反的顺序存储,它们的每个节点都包含一个数字。添加这两个数字并以链表的形式返回。
思路:
(1)l1链表和l2链表从头节点开始相加
(2)如果两节点值之和有进位,用tmp存储进位
(3)下一次两节点值相加时要加上前一次所产生的进位

代码实现:

class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode* ret = new ListNode(1);
        int tmp = 0;
        ListNode* pre = ret;
        while(l1 || l2 ||tmp != 0){
            if(l1){
               tmp+=l1->val;
               l1=l1->next;
            }
            if(l2)
            {
                tmp+=l2->val;
                l2=l2->next;
            }
            ListNode* cur = new ListNode(tmp%10);
            pre->next = cur;
            pre = cur;
            tmp=tmp/10;
        }
        return ret->next;
    }
};

(8)Given a linked list, remove the n th 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.

翻译:删除链表倒数第n个节点
思路:
(1)快指针先走n步;
(2)快慢指针同时向后走,直到快指针指向链表的末尾;
(3)此时慢指针到达链表倒数第n个节点;
(4)删除倒数第n个节点,返回链表头节点;

代码实现:

/**
 * 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) {
           if(head == NULL){
            return head;
        }
        ListNode* ret = new ListNode(0);
        ret->next = head;
        head = ret;
        ListNode* slow = head;
        ListNode* fast = head;
        for(int i = 0; i <n; i++){//快指针先走n步
            fast = fast ->next;
        }
        while(fast->next != NULL){//快慢指针一起走,直到快指针下一个节点指向空
            fast = fast->next;
            slow = slow->next;
        }
        ListNode* temp = slow->next;//此时慢指针指向的下一个节点即为要删除的节点
        slow ->next = slow->next->next;
        delete temp;
        return ret->next;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值