php版两链表对应相加

题目

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
这里学习了来自网络leetcode里面的算法题

//php演示链表需要定义node节点
class listNode 
{
	public $val = 0;
	public $next = null;
	function __construct ($val) {
		$this->val = $val;
	}
}

class solution 
{
	function addTwoNumbers ($l1, $l2) {
		$add = 0;
		$list = new listNode(0);
		$cur = $list;
		while ($l1 || $l2) {
			$x = $l1 != null ? $l1->val : 0;
			$y = $l2 != null ? $l2->val : 0;
			$val = ($x + $y + $add)%10;
			$add = ($x + $y + $add)/10;
			$new = new listNode($val);
			$cur->next = $new;
			$cur = $cur->next;
			if ($l1 != null) {
				$l1 = $l1->next;
			}
			if ($l2 != null) {
				$l2 = $l2->next;
			}	
		}
		if ($add > 0) {
		    $cur->next = new listNode($add);
		}
		return $list->next;
	}
}
要定义两个链表并实现两个链表对应元素相加,需要先定义链表节点结构体struct ListNode,包括节点值val和指向下一个节点的指针next。然后可以使用createList函数创建两个链表,并使用addTwoNumbers函数将它们相加,最后使用printList函数打印结果链表。 具体实现步骤如下: 1. 定义链表节点结构体struct ListNode,包括节点值val和指向下一个节点的指针next。 2. 定义创建节点的函数createNode,该函数接受一个整数参数,返回一个新的链表节点。 3. 定义创建链表的函数createList,该函数接受一个整数数组参数,返回一个新的链表。 4. 定义打印链表的函数printList,该函数接受一个链表参数,将链表中的元素打印出来。 5. 定义两个指针l1和l2分别指向两个链表的头节点。 6. 使用一个循环遍历两个链表,将对应节点的值相加,并将结果存储到新的链表中。需要注意的是,如果相加的结果大于等于10,则需要进位。 7. 最后,如果最高位有进位,则需要在新链表的末尾添加一个节点。 下面是一个示例代码: ``` #include <stdio.h> #include <stdlib.h> struct ListNode { int val; struct ListNode *next; }; struct ListNode* createNode(int val) { struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL; return node; } struct ListNode* createList(int* nums, int size) { struct ListNode* head = NULL; struct ListNode* tail = NULL; for (int i = 0; i < size; i++) { struct ListNode* node = createNode(nums[i]); if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; } void printList(struct ListNode* head) { while (head != NULL) { printf("%d ", head->val); head = head->next; } printf("\n"); } struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode* head = NULL; struct ListNode* tail = NULL; int carry = 0; while (l1 != NULL || l2 != NULL) { int val1 = (l1 != NULL) ? l1->val : 0; int val2 = (l2 != NULL) ? l2->val : 0; int sum = val1 + val2 + carry; carry = sum / 10; struct ListNode* node = createNode(sum % 10); if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } if (l1 != NULL) l1 = l1->next; if (l2 != NULL) l2 = l2->next; } if (carry > 0) { struct ListNode* node = createNode(carry); tail->next = node; tail = node; } return head; } int main() { int nums1[] = {2, 4, 3}; int nums2[] = {5, 6, 4}; struct ListNode* l1 = createList(nums1, 3); struct ListNode* l2 = createList(nums2, 3); struct ListNode* result = addTwoNumbers(l1, l2); printList(result); return 0; } --相关问题--:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值