LintCode:链表求和

描述:

你有两个用链表代表的整数,其中每个节点包含一个数字。数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头。写出一个函数将两个整数相加,用链表形式返回和。


样例:

给出两个链表 3->1->5->null 和 5->9->2->null,返回 8->0->8->null


代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    /**
     * @param l1: the first list
     * @param l2: the second list
     * @return: the sum list of l1 and l2 
     */
    ListNode *addLists(ListNode *l1, ListNode *l2) {
        // write your code here
        int add_Bit = 0;
        ListNode *m_head,*p;
        m_head = (ListNode*)malloc(sizeof(ListNode));
        p = m_head;
        p -> next = NULL;
        p -> val = 0;
        while(l1||l2){
            if(l1 && l2){
                p -> val = (l1 -> val + l2 -> val + add_Bit) % 10;
                add_Bit = (l1 -> val + l2 -> val + add_Bit) / 10; 
                l1 = l1 -> next;
                l2 = l2 -> next;
            }
            else if(l1 && !l2){
                p -> val = (l1 -> val + add_Bit) % 10;
                add_Bit = (l1 -> val + add_Bit) / 10;
                l1 = l1 -> next;
            }
            else if(!l1 && l2){
                p -> val = (l2 -> val + add_Bit) % 10;
                add_Bit = (l2 -> val + add_Bit) / 10;
                l2 = l2 -> next;
            }
            p -> next = (ListNode*)malloc(sizeof(ListNode));
            if(l1||l2){
                p = p -> next;
            }
        }
        if(add_Bit){
            p = p -> next;
            p -> val = 1;
        }
        p -> next = NULL;
        return m_head;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++用数组的思想实现链表基本功能的示例代码: ```c++ #include <iostream> using namespace std; const int MAX_SIZE = 100; //链表的最大长度 struct Node { //定义链表节点结构体 int data; //数据域 int next; //指针域,存储下一个节点的下标 }; class LinkedList { private: Node nodes[MAX_SIZE]; //存储链表节点的数组 int head; //头节点的下标 int tail; //尾节点的下标 int length; //链表长度 public: LinkedList(); //构造函数 bool insert(int pos, int data); //在链表的pos位置插入数据data bool remove(int pos); //删除链表中pos位置的节点 int search(int data); //在链表中查找数据data,返回其位置 void traverse(); //遍历链表 }; LinkedList::LinkedList() { head = -1; //头节点的下标初始化为-1,表示链表为空 tail = -1; //尾节点的下标初始化为-1,表示链表为空 length = 0; //链表长度初始化为0 } bool LinkedList::insert(int pos, int data) { if (pos < 0 || pos > length) { //插入位置不合法 return false; } if (length == MAX_SIZE) { //链表已满,无法插入 return false; } int newNodeIndex = length; //新节点的下标为当前链表长度 nodes[newNodeIndex].data = data; //设置新节点的数据域 if (pos == 0) { //插入位置为链表头部 nodes[newNodeIndex].next = head; //新节点的指针域指向原头节点 head = newNodeIndex; //更新头节点下标 } else if (pos == length) { //插入位置为链表尾部 nodes[tail].next = newNodeIndex; //原尾节点的指针域指向新节点 nodes[newNodeIndex].next = -1; //新节点的指针域指向-1,表示它是尾节点 tail = newNodeIndex; //更新尾节点下标 } else { //插入位置为链表中间 int currNodeIndex = head; //从头节点开始遍历链表 int prevNodeIndex = -1; //记录当前节点的前一个节点的下标 for (int i = 0; i < pos; i++) { //遍历链表,找到要插入的位置 prevNodeIndex = currNodeIndex; currNodeIndex = nodes[currNodeIndex].next; } nodes[newNodeIndex].next = currNodeIndex; //新节点的指针域指向当前位置的节点 nodes[prevNodeIndex].next = newNodeIndex; //前一个节点的指针域指向新节点 } length++; //链表长度加1 return true; } bool LinkedList::remove(int pos) { if (pos < 0 || pos >= length) { //删除位置不合法 return false; } int currNodeIndex = head; //从头节点开始遍历链表 int prevNodeIndex = -1; //记录当前节点的前一个节点的下标 for (int i = 0; i < pos; i++) { //遍历链表,找到要删除的位置 prevNodeIndex = currNodeIndex; currNodeIndex = nodes[currNodeIndex].next; } if (pos == 0) { //删除头节点 head = nodes[head].next; //更新头节点下标 } else if (pos == length - 1) { //删除尾节点 tail = prevNodeIndex; //更新尾节点下标 nodes[tail].next = -1; //尾节点的指针域指向-1,表示它是尾节点 } else { //删除中间节点 nodes[prevNodeIndex].next = nodes[currNodeIndex].next; //前一个节点的指针域指向当前节点的下一个节点 } length--; //链表长度减1 return true; } int LinkedList::search(int data) { int currNodeIndex = head; //从头节点开始遍历链表 for (int i = 0; i < length; i++) { //遍历链表,查找数据data if (nodes[currNodeIndex].data == data) { //找到数据 return i; //返回数据所在位置 } currNodeIndex = nodes[currNodeIndex].next; //指向下一个节点 } return -1; //未找到数据 } void LinkedList::traverse() { int currNodeIndex = head; //从头节点开始遍历链表 while (currNodeIndex != -1) { //遍历链表,直到尾节点 cout << nodes[currNodeIndex].data << " "; //输出当前节点的数据 currNodeIndex = nodes[currNodeIndex].next; //指向下一个节点 } cout << endl; } int main() { LinkedList list; list.insert(0, 1); list.insert(1, 2); list.insert(2, 3); list.insert(1, 4); list.traverse(); //输出:1 4 2 3 list.remove(2); list.traverse(); //输出:1 4 3 cout << list.search(4) << endl; //输出:1 cout << list.search(5) << endl; //输出:-1 return 0; } ``` 以上代码中,链表的节点采用结构体实现,并存储在一个数组中。头节点的下标为head,尾节点的下标为tail,链表长度为length。链表的插入、删除、查找和遍历操作都使用数组的索引来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值