1. 연결리스트 기본 구조
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
2. 연결리스트 값을 기준으로 삭제
- head를 삭제하는 경우
- 중간 부분을 삭제하는 경우
- 마지막을 삭제하는 경우로 나뉜다.
def deleteNode(self,head,value):
#가장 첫번째 값을 삭제하는 경우
if head.value == value:
return head.next
prev = None
current = head
while current :
if current.val == value:
if current.next is None:
prev.next= None
else :
prev.next= current.next
prev = current
current = current.next
return head'코딩 > 파이썬' 카테고리의 다른 글
| [python]리스트 역순으로 연결리스트 만들기, 연결리스트 만들기 (0) | 2023.07.18 |
|---|---|
| [python]연결리스트 뒤집기 (0) | 2023.07.18 |
| [프로그래머스] 두 개 뽑아서 더하기 (0) | 2023.07.17 |
| [프로그래머스] 문자열 내 마음대로 정렬하기,python (0) | 2023.07.17 |
| [Python] Unindent amount does not match previous indentPylance 에러 (0) | 2023.07.17 |