[LeetCode] Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

begin to intersect at node c1.

  • 解题思路
    首先把两个链表的所有非空结点入栈,然后比较栈顶元素并出栈,直到一个栈为空或者栈顶元素不相等,此时上一次比较的结点即为相交结点。
  • 实现代码
/*************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/8 13:33
    *  @Status   : Accepted
    *  @Runtime  : 76 ms
*************************************************************/
#include <vector>
#include <iostream>
#include <stack>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        stack<ListNode*> stack1;
        stack<ListNode*> stack2;
        ListNode* pa = headA;
        ListNode* pb = headB;
        while(pa)
        {
            stack1.push(pa);
            pa = pa->next;
        }
        while(pb)
        {
            stack2.push(pb);
            pb = pb->next;
        }

        ListNode* temp = NULL;
        while (!stack1.empty() && !stack2.empty())
        {
            if (stack1.top()->val == stack2.top()->val)
            {
                temp = stack1.top();
                stack1.pop();
                stack2.pop();
            }
            else
            {
                return temp;
            }
        }
        return temp;
    }
};

void addListNode(ListNode*& head, int i)
{
    ListNode* h = head;
    while(h->next != NULL)
    {
        h = h->next;
    }
    ListNode* temp = new ListNode(i);
    h->next = temp;
}

//void print(ListNode* head)
//{
//  ListNode* temp = head;
//  while(temp)
//  {
//      cout<<temp->val<<endl;
//      temp = temp->next;
//  }
//}

int main()
{
    Solution s;
    ListNode* headA = new ListNode(0);
    addListNode(headA, 1);
    addListNode(headA, 2);
    addListNode(headA, 1);
    addListNode(headA, 2);
    addListNode(headA, 3);

    ListNode* headB = new ListNode(0);
    addListNode(headB, 1);
    addListNode(headB, 2);
    addListNode(headB, 3);
    addListNode(headB, 1);
    addListNode(headB, 2);
    addListNode(headB, 3);

    ListNode* p = s.getIntersectionNode(headA, headB);
    cout<<p->val<<endl;
    system("pause");
}
时间: 2024-10-25 00:15:50

[LeetCode] Intersection of Two Linked Lists的相关文章

[LeetCode]160.Intersection of Two Linked Lists

[题目] Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have

【LeetCode从零单排】No.160 Intersection of Two Linked Lists

题目 Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have n

LeetCode 23 Merge k Sorted Lists(合并K个已排序链表)

翻译 合并K个已排序的链表,并且将其排序并返回. 分析和描述其复杂性. 原文 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 代码 我们采用分治的方法来解决这个问题,其有K个链表,不断将其划分(partition),再将其归并(merge). 划分的部分并不难,将其不断分成两部分,但是需要注意的是可能出现start和end相等的情况,这时候就直接r

[LeetCode]21.Merge Two Sorted Lists

[题目] Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. [分析] 无 [代码] /********************************* * 日期:2015-01-06 * 作者:SJF0115 * 题目: 21.Merge Two Sorted L

[LeetCode]23.Merge k Sorted Lists

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [分析] 无 [代码] /********************************* * 日期:2015-01-06 * 作者:SJF0115 * 题目: 23.Merge k Sorted Lists * 来源:https://oj.leetcode.com/problems/me

LeetCode 21 Merge Two Sorted Lists(合并两个已排序的数组)

翻译 合并两个排好序的链表,并返回这个新链表. 新链表应该由这两个链表的头部拼接而成. 原文 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 代码 /** * Definition for singly-linked list. * struct ListNode

struct use case - linked lists

在说linked list数据结构前, 来想象一个场景. 假设要设计一个旅游线路,依次游览几个岛屿.如图,   第一种设计思路如下 :  typedef struct { char * name; char * opens; char * closes; } // 使用数组存储岛屿信息, 行程按照数组下标顺序进行. island tour[4]; 但是这里有两个明显的问题 :  1. 扩展性问题, 如果临时改变行程增加游览的岛屿怎么办? 比如现在是游览4个岛屿, 要变成5个. 2. 灵活性问题,

[LeetCode] Intersection of Two Arrays

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. 解题思路 首先用map存储数组1里出现的元素,然后检查这些元素是否也在数组2

[LeetCode 第10题] -- Linked List Cycle

题目链接: linked List Cycle 题目意思: 给定一个链表,判断链表是否有环 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head); }; b