본문 바로가기

연결리스트9

[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: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 Output: Reference of the node with value = 8 Input Explanation: The intersected node's value is 8 (note that this must not be 0.. 2020. 11. 2.
[leetcode 234] Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true Follow up: Could you do it in O(n) time and O(1) space? 문제 풀이: 첨에 follow up을 무시하고 풀었다. 어떻게 거꾸로 돌지?해서 무시함 ㅇㅇ 첫 번째 방법(c++): 연결리스트를 모두 벡터에 집어넣고 전체 길이가 홀수/짝수일때를 나눠서 가운데부터 비교한다. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *n.. 2020. 10. 31.
[leetcode 138] Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. The Linked List is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where: val: an integer representing Node.val random_index: the index of the node (range from 0 to n-1) .. 2020. 10. 21.
[leetcode 21] Merge Two Sorted Lists Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. 오름차순순으로 리스트를 합치자. Example 1: Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: l1 = [], l2 = [] Output: [] Example 3: Input: l1 = [], l2 = [0] Output: [0] 풀이: -포인터만 잘 활용한다면 쉬운 문제이다. - 난이도: easy /** * Definition for singly-lin.. 2020. 10. 17.