203.移除链表元素;707.设计链表;206.反转链表
Definition of Linked List
class ListNode:
def __int__(self,val=0,next=None):
self.val=val
self.next=next
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]
Sol:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
dummy_head = ListNode(next=head) #dummy head to simplify real head process
current = dummy_head
while current.next: #the loop is all about .next, so check .next
if current.next.val == val:
current.next = current.next.next
else:
current = current.next
return dummy_head.next
- Note:
- dummy_head: to make the real head same as other nodes, simple for process
- “while current.next” to check next so to use “.next.val”
- return “dummy_head.next”: the real head
============================================
707. Design Linked List
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
Input
[“MyLinkedList”, “addAtHead”, “addAtTail”, “addAtIndex”, “get”, “deleteAtIndex”, “get”]
[[], [1], [3], [1, 2], [1], [1], [1]]
Output
[null, null, null, null, 2, null, 3]
Explanation
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
myLinkedList.get(1); // return 2
myLinkedList.deleteAtIndex(1); // now the linked list is 1->3
myLinkedList.get(1); // return 3
Sol:
class ListNode:
def __init__(self, val=0, next=None):
self.val=val
self.next=next
class MyLinkedList:
def __init__(self):
self.dummy_head=ListNode() #dummy head: simplify add and remove
self.size=0
def get(self, index: int) -> int:
if index<0 or index>self.size-1:# due to dummy head, real size is size-1
return -1
current = self.dummy_head.next
for i in range(index):
current = current.next
return current.val
def addAtHead(self, val: int) -> None:
head = self.dummy_head.next
self.dummy_head.next = ListNode(val,head)
self.size+=1
def addAtTail(self, val: int) -> None:
current = self.dummy_head
for i in range(self.size):
#while current.next:
current = current.next
current.next = ListNode(val,None)
self.size+=1
def addAtIndex(self, index: int, val: int) -> None:
if index<0 or index>self.size:
#"add operation okay with "self.size", meaning insert at tail"
return
current = self.dummy_head
for i in range(index):
current = current.next
current.next=ListNode(val,current.next)
self.size+=1
def deleteAtIndex(self, index: int) -> None:
# delete work on real size: self.size-1
if index<0 or index>self.size-1:
return
current = self.dummy_head
for i in range(index):
current = current.next
current.next=current.next.next
self.size-=1
# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
- Notes:
- dummy head: simplify add and delete
- addAtIndex: okay with size index=self.size-1. this is insert tail
- other operations: real size = self.size-1
- remember to update self.size for each operation
============================================
206. Reverse Linked List
Given the head of a singly linked list, reverse the list, and return the reversed list.
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Sol
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
current=head
prev=None
while current:
temp=current.next
current.next = prev
prev = current
current = temp
return prev
- Notes: reverse donot use dummy head because head changes side, dummy head becomes a tail causing error.