linkedlist (2) 썸네일형 리스트형 [Easy] 141. Linked List Cycle Leetcode 141번 문제는 주어진 Linkedlist에 cycle ,즉 순환인 발생하는지 알아내는 문제이다. 위의 예제처럼 어느 한 노드에서 뒤에 노드로 뒤돌아가 반복된다면 cycle이 발생한 것이므로 true를 return 하면 된다. #풀이 Hashset을 사용한 풀이 public class Solution { public boolean hasCycle(ListNode head) { HashSet set = new HashSet(); boolean isCycle = false; while(head != null){ isCycle = set.add(head); head = head.next; if(!isCycle){ return !isCycle; } } return !isCycle; } } Set.. [Easy] 206. Reverse Linked List Leetcode 206번 문제는 주어진 연결 리스트를 반대로 뒤집는 문제이다. 연결 리스트의 reference to the head가 주어진다. #풀이 # Iteratively (반복) 풀이 class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; while(head != null){ ListNode nextNode = head.next; head.next = prev; prev = head; head = nextNode; } return prev; } } # recursively (재귀적) 풀이 class Solution { public ListNode reverseList(ListNode head) { //.. 이전 1 다음