목록연결 리스트 (7)
N
https://leetcode.com/problems/merge-nodes-in-between-zeros/ 주어진 연결 리스트에서 0으로 된 노드 사이의 값을 더하여 새로운 연결 리스트로 반환하면 된다. 우선 answer를 new ListNode()로 선언한다. 그리고 head 내부에 있는 값을 배열에 저장하기 위해 arr 배열을 선언. 0 노드 사이의 값을 더하기 위한 sum과 포인터인 current를 선언한다. current는 head를 가리켜 arr에 값을 저장하도록 한다. while문으로 head를 한 번 순회한다. current !== null 때 까지 반복. 순회를 할 때에는 노드의 값을 더하여 arr에 저장한다. 0이 아닌 수는 sum에 더하고, 만약 노드에서 0을 만나게 되고 sum이 0이..
https://leetcode.com/problems/swapping-nodes-in-a-linked-list/ Swapping Nodes in a Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 연결 리스트. 앞에서 k번째에 있는 노드와 뒤에서 k 번째에 있는 노드를 서로 바꿔주면 된다. array를 이용하여 문제 해결. arr 배열 안에 head에 있는 노드를 모두 넣어준다. 이후 p1 = k - 1, p2 = arr.length - ..

24. Swap Nodes in Pairs Medium 4452236Add to ListShare Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) Example 1: Input: head = [1,2,3,4] Output: [2,1,4,3] Example 2: Input: head = [] Output: [] Example 3: Input: head = [1] Output: [1] Constraints: The..
23. Merge k Sorted Lists Hard 8515382Add to ListShare You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Example 1: Input: lists = [[1,4,5],[1,3,4],[2,6]] Output: [1,1,2,3,4,4,5,6] Explanation: The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted list: 1->1->..

Doubly Linked List 양방향으로 이어지는 연결 리스트. Singly Linked List보다 메모리 크기가 조금 더 크다.(이전 노드를 가리키는 메모리가 있어야 하기 때문이다!) 데이터 추가 31을 6과 2 사이에 추가해보자. 우선 새롭게 만든 31의 다음 노드를 6의 다음 노드인 2로 연결한다. 2의 이전 노드는 31을 가리키도록 한다. 6의 다음 노드를 31로 연결한다. 31의 이전 노드를 6으로 연결한다. 단방향 연결 리스트보다 복잡하게 느껴지겠지만, 이해한다면 쉽게 다룰 수 있다! 데이터 삭제 6을 지웠을 경우다. 31의 이전 노드를 6의 이전 노드인 1을 가리키도록 한다. 1의 다음 노드를 31로 바꾼다. 6을 삭제한다. 코드 class Node { constructor(value)..

연결 리스트 각 요소를 포인터로 연결하여 관리하는 선형 자료구조. 각 요소는 노드라고 부르며, 데이터 영역과 포인터 영역으로 구성된다. 메모리가 허용하는 한 요소를 제한없이 추가할 수 있다. 탐색은 O(n)이 소요. 요소를 추가하거나 제거할 때는 O(1)이 소요. Singly Linked List, Doubly Linked List, Circular Linked List가 존재. 배열과의 차이점 배열은 데이터가 메모리에 연속적으로 붙어있지만, 연결 리스트는 붙어있지 않다. 원소를 찾기 위해서 배열은 인덱스로 접근이 가능하지만, 연결 리스트는 해당 데이터 영역을 찾아야 한다.(그렇기 때문에 에 시간 복잡도가 서로 상반된다.) Singly Linked List 단방향 연결 리스트 Head에서 Tail까지 단..

203. Remove Linked List Elements Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head. Example 1: Input: head = [1,2,6,3,4,5,6], val = 6 Output: [1,2,3,4,5] Example 2: Input: head = [], val = 1 Output: [] Example 3: Input: head = [7,7,7,7], val = 7 Output: [] Constraints: The number of nodes in the list is i..