728x90
    
    
  [Javascript] LeetCode 876. Middle of the Linked List
💡 구글에 Javascript 풀이가 많이 없거나, 배운 점이 있으면 포스팅합니다.
내 풀이
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var middleNode = function(head) {
    let node1 = head;
    let node2 = head;
    while (node2 && node2.next) {
        node1 = node1.next;
        node2 = node2.next.next; 
    }
    return node1;
};- Two pointer기법으로 풀었습니다. 이전에 JAVA로 한번 풀어본 거라 풀이가 기억나서 쉽게 풀었습니다.
배운 점, 느낀 점
- 자료구조의 시작이 보통 연결리스트인데, 만날때마다 헷갈리네요. 많이 풀어봐야겠습니다. 그래프까진 마스터해야 자료구조에 대해 안다고 말할 수 있지 않을까…하는 생각이 있습니다.
'Coding Test > LeetCode' 카테고리의 다른 글
| [Javascript] (LeetCode)122. Best Time to Buy and Sell Stock II (Medium) (0) | 2022.07.22 | 
|---|---|
| [Javascript] (LeetCode)121. Best time to buy and sell stock (Easy) (0) | 2022.07.21 | 
| [Javascript] (LeetCode) 26. Remove Duplicates from Sorted Array (Easy) (0) | 2022.07.20 | 
| [Javascript] (LeetCode) 19. Remove Nth Node From End of List (Medium) (0) | 2022.07.14 | 
| [Javascript] LeetCode 557. Reverse Words in a String III (0) | 2022.07.13 | 
| [Javascript] LeetCode 344. Reverse String (0) | 2022.07.13 | 
| [Javascript] LeetCode 167. Two Sum II - Input Array Is Sorted (0) | 2022.07.13 | 
| 오름차순 (Ascending order) vs 비 내림차순(non-decreasing order) (0) | 2022.07.13 | 
댓글